Service terminates when activity is closed

I read a bunch of answers related to this question, and they all seem to be the same:

"Start the service using START_STICKY"
"Run the service in the foreground"
"Start the service using startService and do not bind it"

I do ALL of these things, and my STILL service closes and restarts every time my activity closes.

This is not an IntentService. I also do not call stopSelf or stopService anywhere except onClick handlers.

Scroll down to my update. This behavior was confirmed as a bug in the Android OS, and I reported it to Google. Click here to view the report.

Starting my service from MainActivity:

svcIntent = new Intent(getBaseContext(), MyService.class); startService(svcIntent); 

In my onStartCommand:

  // Enter foreground state String title = "Service has been started..."; String subject = "Service is running..."; String body = "Monitoring your battery usage."; Notification notification = new Notification(R.drawable.theicon, title, System.currentTimeMillis()); if (prefs.getBoolean("notificationSounds", true)) notification.defaults |= Notification.DEFAULT_SOUND; else notification.sound = null; Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, subject, body, pendIntent); startForeground(1500, notification); 

At the end of my onStartCommand:

 ... // Release WakeLock wl.release(); return START_STICKY; 

UPDATE

I CONFIGURED WHAT YOU ACCEPT! But I have no idea how to fix this. In my service, I also use AlarmManager inside my service to configure function calls for the service at a specified time.

  // Alarm manager setup for MyService AlarmManager AM = (AlarmManager) getSystemService(Context.ALARM_SERVICE); svcIntent1 = new Intent(this, AlarmReceiver.class); prefs.edit().putInt("initialBatt", initialBatt).apply(); svcIntent1.setAction("com.myApp.servicealarm"); pendingIntent = PendingIntent.getBroadcast(this, 93, svcIntent1, PendingIntent.FLAG_UPDATE_CURRENT); // Set the alarm AM.set(AlarmManager.RTC_WAKEUP, timeNow + waitTime, pendingIntent); 

I noticed that if I DO NOT CONCLUDE calling AM.set to set the alarm, EVEN WITH EMPTY ONReceive, my service will be killed when the alarm goes off after I deleted the application in recent applications. If I comment on the set alarm, the service will never be killed and will continue to work after closing my application. What the heck?! I need this alarm for the functionality of my algorithms!

It is very strange. As soon as the alarm goes off, my debugging message does not print, and my service restarts. But the second time, after the service is restarted, the debug message prints, and the program runs successfully.

I tried this, and it still happens with a normal broadcast receiver. I also disconnected my code before the ONLY set alarm from my service and broadcast receiver, and the same thing happens, so this is not my algorithm. Obviously, if you have a front-end service that sets an alarm, when the alarm goes off, your service restarts.

closing

This behavior is apparently caused by an error in the Android OS, so I do not expect an answer. If you want to see this error yourself, click here . I have provided a project with which you can compile and reproduce the problem.

+5
source share
2 answers

Android kills the process when an Intent transfer is sent (before it is received / processed in your application) .

This is a nasty Android bug starting with 4.4 (API 19).

See https://code.google.com/p/android/issues/detail?id=63618&can=1&q=service%20restart%20broadcast&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars

especially comments # 22 and # 23

Unfortunately, almost all "open" problems have been marked as "obsolete" recently, provided that they were all fixed in Android 5.0. The developer has no way to resume the "obsolete" problem.


EDIT: Add Broadcast Information

Based on the information in the related issue, it seems that adding Intent.FLAG_RECEIVER_FOREGROUND to your Intent broadcast ensures that the process will not be killed the next time the Intent broadcast is received.

To do this, add:

 svcIntent1.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); 

into the code in which you set the alarm in the AlarmManager .

Read the comments in the related issue for more details.

+4
source

Try starting the service in a separate process. Define this in your manifest as follows:

 <service android:name=".path.to.service.class" android:process=":my_service"/> 
0
source

Source: https://habr.com/ru/post/1211554/


All Articles