I have a background service in my android application.In which a thread listening recent tasks running frequently.My service overrides both onCreate()
and onStartCommand()
methods. When i tried to open some applications like Gallery
,Camera
etc..., the service will be stopped.It calls the onCreate()
method only and not onDestroy()
or onStartCommand()
.I tried to override onLowMemory() in this service but it logs nothing.The application saved internally by specifying
android:installLocation="internalOnly"
in the manifest file.
Note: This issue noticed on Micromax A54 2.3.5
.
Why does the background service stops sometimes? Is there any solution for this issue?
public class MyService extends Service {
public MyService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
if(intent != null){
//......
}
} catch (Throwable e) {
}
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onCreate() {
super.onCreate();
//......
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
And start the service like this
Intent intent = new Intent(getApplicationContext(), MyService.class);
getApplicationContext().startService(intent);
Android can stop any service at any time for any reason. A typical reason is low memory (killing the service to give its memory elsewhere), but I've seen at least a few devices that kill them every X hours regardless. There is no way to ensure you always run. The best you can do is have all your activities try to start your service (in case it isn't running), write your service so it can reload needed data, and set yourself as START_STICKY so if it has enough memory the framework will restart you later.