background services for getting activity recognition - android

I read a lot about services I tried a lot of examples but unfortunately I couldn't understand how could I keep always services running to get activity recognition API always up and running.
In all cases Android OS killing somehow my running service.
What I tried:
I put START_STICKY but when it killed never runs again
#Override
public int onStartCommand(Intent intent, final int flags, int startId) {
// my code is here
return START_STICKY;
}
I started service again when I detected that service was killed. It not worked good as expected. I am sending broadcast and starting the service again.
#Override
public void onDestroy() {
sendBroadcast(new Intent("YouWillNeverKillMe"));
super.onDestroy();
}
I know about foreground services but in this case there is no way I can use foreground services. I have to forget about this solution.
Is there any other way you can suggest me as a solution? What exactly doing there Facebook or Viber that always I getting messages?

Related

Is is possible to run Android Service after app deinstallation?

i want to run a script even if the Task is destroyed. That works fine but is it possible to keep this service running, after the user destroyes the app?
I read something about binding the service but this is not working for me.
No. If the user uninstalls the app, all components are destroyed and removed from the operating system.
However, you can make a Service automatically restart after the app is killed (but not uninstalled) by starting through context.startService(Intent), and returning Service.START_STICKY in onStartCommand()
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}

How to run android service permanently like system service?

I want to run my Android service(real-time notification service) after application installed and force it to work even application closed. This service responsible for listening my real-time notification server, when notification received I create Android notification and maybe run my app, if it is closed. I assume that my way is not correct for this case and there is another way working with real-time notification. I will be grateful if you give some link or a small explanation.
the better approach to listening my real-time notification server, you've to use GCM. here you can start reading about GCM. and if you want to implement it yourself you have to write a service yourself. ex:
public class ListenerService extends Service{
#Override
public void onStartCommand(Intent intent, int flags, int startId){
return START_STICKY; //this will restart your service even though the system kills
}
// you can write other listener service method inside the service
}
but still I recommend you to use GCM or other cloud messaging services instead of writing your own.
Extend the Android Service class in your class which you want to run in background always.Override the following method and return START_STICKY which makes your service to always run in background until you stop it.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//your code here
return START_STICKY;
}
If you need to do any long running task than create a seperate thread for it and use it inside the service because service runs on main thread.

Prevent Android App in background getting suspended

I wrote an App that will run process in a long time, possibly never ending until the user stop it.
But I afraid the App will get suspended when the mobile/tablet screen is sleep, or user switch to another app so that my app long running task is getting suspended.
How do I prevent that?
You can probably start a Service here if you want your Application to run in Background. This is what Service in Android are used for - running in background and doing longtime operations.
You can use START_STICKY to make your Service running continuously.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
and see this :http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
and This is an example of background service:
http://marakana.com/forums/android/examples/60.html
What you want is implement a Service.
It will have that behavior that you are asking.
You should use a Service and return START_STICKY on the onStartCommand() so that the system will not terminate your app. Even if it is terminated, the service is automatically restarted.

WhatsApp background running system

I am interested to know how WhatsApp runs in Android background system even after cleaning it from ram cleaner.
I made an Android app in which I started service and broadcast receiver but when I cleaned it using ram cleaner, both got stopped. Even sometimes push notifications are also not received when app is not running in background.
So, I just wanted to know that how WhatsApp manages all this. I am just giving an example as WhatsApp because I found its system amusing.
If you return START_STICKY from onStartCommand(), the system will automatically restart the service once it determines that it is not resource strained. Which means it will probably restart immediately if you have killed it using an app killer.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
http://developer.android.com/reference/android/app/Service.html#START_STICKY

How to restart Service after Force Stop of app

Here is my Service
public class SService extends Service {
public void onCreate() {
super.onCreate();
someTask();
}
public int onStartCommand(Intent intent, int flags, int startId) {
someTask();
return START_STICKY;
}
public IBinder onBind(Intent intent) {
return null;
}
After force stop of app (Settings->Application->Force Stop App) my Service doesn't run. How to solve it?
As per the Android document
Starting from Android 3.1, the system's package manager keeps track of applications that
are in a stopped state and provides a means of controlling their launch from background
processes and other applications.
Note that an application's stopped state is not the same as an Activity's stopped
state. The system manages those two stopped states separately.
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It
does this to prevent broadcasts from background services from inadvertently or unnecessarily
launching components of stoppped applications. A background service or application can
override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents
that should be allowed to activate stopped applications.
On Force stop of app, Android just kill the process ID. No warnings, callbacks are given to service/activities. As per the Android document, When the app is killed there are chances that it calls onPause().
When I tried in my app, even onPause() was not called.
I think the only way is use to that intent flag and send it from another app as suggested by Chris.
Are you sure that the service isn't restarting? The START_STICKY you put in the return should do the trick some time. It takes some time till the system restart it, you can put a log and wait to make sure it's getting restarted.

Categories

Resources