I've been trying this for a long time, looking for documentation and different questions on stackoverflow but I haven't found the key.
I need to launch a service that doesn't die when closing the APP completely.
I have tried
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
with the permission:
<uses-permission android:name="android.permission.WAKE_LOCK" />
When launching it:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock cpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
cpuWakeLock.acquire();
Intent intent = new Intent(this, WebViewService.class);
bindService(intent, serviceConnection, BIND_AUTO_CREATE);
And endless codes that have not worked... is it possible?
Any advice? Thank you!
I have implemented something similar in my own application that what it does is start the service every time it dies and also when the device is restarted:
Class that extends BroadcastReceiver:
In my service itself:
In the AndroidManifest.xml:
Edit: The service is started in the onCreate of my MainActivity:
To keep a service when closing the application, use
START_STICKY
inonStartCommand()
According to your code I see that you link a service so what you are doing is that your Activity interacts with the service:
In this case, what you could do is use a service in the Foreground ( Foreground Service ), which is done through the startForeground() method :
The option that I perform regularly through a BroadcastReceiver is the following, you can take this article as an example:
http://androidtrainningcenter.blogspot.mx/2013/05/how-to-make-android-service-unstoppable.html
1) register the service when you start your device, with this you don't have to open the application for it to start:
2) Add in the class
BroadcastReceiver
code to restart the service again.3) Create the Android Service class and in
onDestroy()
implement start the service and definereturn START_STICKY
inonStartCommand()
so that the service continues to work after closing the application.@Andress Blend for those who need the isMyServiceRunning method.