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.
Its not working, i tried on many phones.it is not running when force stop. Please suggest me asap...
ReplyDeleteIts 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
ReplyDeleteThanks for quick reply sameer,
ReplyDeleteYour 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.
I tried to force stop application while testing, The service restart itself. I donot why it behaving in that way with you
ReplyDeleteForce 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
ReplyDeleteThanks for your code its working but the app killer like NQ Android Booster,CCleaners are closing the running applications
ReplyDeleteIs it not restarting?
ReplyDelete