Search This Blog

Saturday, 4 May 2013

How to make Android service unstoppable and run it continuously

Android service runs in different mode with help of flag e.g START_CONTINUATION_MASK, START_NOT_STICKY. It enable to control the behavior of Android service life. But service will work until user allow it work. If he/she stops it, it will destroy automatically. More than often, we required it to run unstoppable  If in case user stop, re-instance the old service instance.
For some people, it may malware but sometimes we have to full filled client requirement and that's all our purpose is.

Case when your service can be stopped



  • User stop it forcefully
  • Low Memory situation
  • Device restarted


Handling first two case


When ever we stop service forcefully (or os kill it), it will call onDestory() method. First concept is to use one receiver and send one broadcast whenever service destroy. And restarted service again.

Third case if device is restarted then already we had onBootCompleted action for receiver to catch

Lets go steps by Step to make our Android Service unstoppable

Step 1) Create one Android BroadCastReciever and register it for two action


Manifest

        <service android:name="ServiceTest" >
        </service>

        <receiver android:name="ReceiverCall" >
            <intent-filter>
                <action android:name="com.android.techtrainner" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

com.android.techtrainner is the custom action. BroadCastReceiver Class contain code to restart service again

public class ReceiverCall extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("Service Stops", "Ohhhhhhh");
        context.startService(new Intent(context, ServiceTest.class));;
    }

}


Step 2) Create Android service class , do some task there (I have taken one Timer to print Log) and in onDestory()


    public void onDestroy() {
        try {
            mTimer.cancel();
            timerTask.cancel();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Intent intent = new Intent("com.android.techtrainner");
        intent.putExtra("yourvalue", "torestore");
        sendBroadcast(intent);
    }

Run your application and go to setting, See running service, you will TestService. Try to close it , ohhhhhh it will not close.

DownloadSource Application



7 comments:

  1. Its not working, i tried on many phones.it is not running when force stop. Please suggest me asap...

    ReplyDelete
  2. Its strange that above code does not work.I posted the thread after checking in my device.Try to go with concept as i explain above.As soon as i got the time , i will recheck code and will notify you

    ReplyDelete
  3. Thanks for quick reply sameer,
    Your app automatically starts after
    boot, but when i force stop the application in android application
    manager it was in closed mode only.

    Actually i'am trying to do
    app like whats app. If you see in whatsapp application if the user
    force stop the app, the app will automatically starts after some time.

    ReplyDelete
  4. I tried to force stop application while testing, The service restart itself. I donot why it behaving in that way with you

    ReplyDelete
  5. Force stop is not action. onDestroy call when service is being stop. so i am sending one custom action and in broadcast receiver i am restarting again

    ReplyDelete
  6. Thanks for your code its working but the app killer like NQ Android Booster,CCleaners are closing the running applications

    ReplyDelete

Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code