Restarting service when app is killed in 7.0 and above - android

Till Android 6.0 whenever I Kill my app my service gets restarted automatically, but when I tested it in 7.1.1 my service does not gets restart it stops.
I tried restarting the service in activity onDestroy like this,
#Override
protected void onDestroy() {
super.onDestroy();
Util.printMessage("onDestroy.....");
Intent locationIntent = new Intent(getApplicationContext(), SupportService.class);
startService(locationIntent);
Util.printMessage("running...?..." + isMyServiceRunning(SupportService.class));
}
But this solution did not work, can anyone suggest what is the issue or how to start the service again and same for the boot change receiver my broadcast does not get call when device is rebooted.

Change the return type of onStartCommand() with START_STICKY. if the service destroy by system it will start automatically... for more info about START_STICKY Click

Related

Foreground service stops on killing app

I have added below code in onStartCommand of service-
Notification notification = new NotificationCompat.Builder(AbcService.this)
.setContentTitle("xyz")
.setTicker("xyz")
.setContentText("xyz")
.setSmallIcon(R.drawable.mevo_chat_mevopic)
.setOngoing(true).build();
startForeground(AppConstants.ABC,
notification);
and stopForeground(true); inside onDestroy() of service.I expect service to run even when app is destroyed as it is foreground service but i see that sometimes it runs and sometimes it gets stopped.I don't have any other apps open and my device doesn't have memory issue.What could be possible reasons for my foreground service being stopped and how can i resolve this?
If your Service is started by your app then actually your service is running on main process. so when app is killed service will also be stopped. So what you can do is, send broadcast from onTaskRemoved method of your service as follows:
Intent intent = new Intent("com.android.ServiceStopped");
sendBroadcast(intent);
and have an broadcast receiver which will again start a service. I have tried it. service restarts from all type of kills.

Will forcely killing Android sticky service will restart it again?

I'm forcely killing my Android Sticky service from "Running Services". But its not getting start again by the OS automatically as mentioned here.
1 - How soon it is restarted again when forcily closed by user through Running Services?
2 - How soon it is restarted again when OS destroys the sticky service?
So from my experience the answer is NO the OS doesn't call the service again if its forcefully shut down by user via "Running Services". It only calls the service onStartCommand(Intent intent, int flags, int startId) method when application is destroyed (swipe away).
But I found a quick solution, as we know when service is destroyed the onDestroy() method is called. So just initialize the service again like this:
#Override
public void onDestroy() {
super.onDestroy();
startService(new Intent(context, TimerService.class));
Log.v("App", "Service Started Again");
}

Android: Service started on boot, is not restarting when forced stop

I have a service which is successfully started on boot complete using a BroadcastReceiver. But when I am doing a force stop it is not restarting.
I tried starting the service from activity, and tried force stop, but in this case service restarted.
I am using Service.START_STICKY
In other question on stackoverflow, it is mentioned to user BaseContext not ApplicationContext. How do I get BaseContext from a BroadcaseReceiver
START_STICKY restart service, when OS kill it.
If you kill the app, it's not work.

Killing an activity that started a sticky service kills the service too

I am using an empty activity to start a sticky service. I start the service using the following code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent service = new Intent(this, MyService.class);
service.setAction("com.company.test.MyService");
startService(service);
finish();
setContentView(R.layout.activity_transparent);
}
The problem is that if the activity gets killed it will cause the service to restart. Is there a way to avoid this problem?
Note: I am killing the application by swiping it out of the task manager, and not the activity's finish() call.
No, actually. This is a new feature in the Android SDK- killing the app kills all processes connected to the app, and even the app's sticky services will not restart anymore.(EDIT: The service may not restart, depending on the device; apparently sticky services are still a buggy feature on certain versions of android.)
If you want to keep your service perpetually running, you will need to use a ForegroundService, with a persistent notification in the drawer.
Apparently, this is to make sure that no services run without the user's knowledge.

android : stop a sticky service

I am developping a wear app.
I start a sticky service in the wear when I receive a dedicated event.
This service returns START_STICKY in onStartCommand.
My question is : how can I stop definitively this service?
I need it to be restarted if Android stops it because of low memory, but I need to be able to stop it definitively by code.
I tried to stop it using stopService(Intent i) or stopSelf() but the service gets recreated each time.
Thank you for your help.
jn.
Call stopService(intentOfYourService) at onPause() or onDestroy() and give the Intent you created at onCreate() (or elsewhere) to the parameteres.

Categories

Resources