Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to prevent service from restarting after crashing?

Tags:

android

Is there a way to prevent my Service from automatically being restarted by ActivityManager after it "crashes"? In some scenarios, I forcefully kill my service on program exit, but do not want Android to keep restarting it.

like image 684
zer0stimulus Avatar asked Feb 13 '12 19:02

zer0stimulus


People also ask

How do I stop my Android from restarting?

Open Settings and tap on Device care. Click on the three dots in the upper right corner and choose Auto restart. If the option is enabled, disable it and see if this resolves your issue.

Why does my Android keep restarting itself?

In most cases, random restarts are caused by a poor quality app. Try uninstalling apps you don't use. Be sure the apps you do use are reliable, especially the apps that handle email or text messaging. My wife recently discovered her Galaxy S5 would restart sometimes when receiving a text message.

Why do my Android apps restart all over again if I minimize them how do I fix this?

What deep clean does is, whenever you minimize an app , it releases all the memory associated with that app , and hence when you again open that app, it will start as if it is new. To check the 'deep clean' option, go to the developer options of your phone. If it is not enabled please enable it.

How do I fix my phone it wont stop restarting?

Force Fresh Restart Press and hold both the “Power” and “Volume Down” buttons. Do this for about 20 seconds or until the device restarts again. This will often clear the memory, and cause the device to start normally.


1 Answers

This behavior is defined by the return value of onStartCommand() in your Service implementation. The constant START_NOT_STICKY tells Android not to restart the service if it s running while the process is "killed". In other words:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // We don't want this service to continue running if it is explicitly
    // stopped, so return not sticky.
    return START_NOT_STICKY;
}

HTH

like image 155
devunwired Avatar answered Oct 26 '22 00:10

devunwired