The service process is destroyed after the application is removed from the application tray.

I start the service (or restart the running service) when the action starts, using:

Intent intent = new Intent(this, MyService.class); startService(intent);

Later, based on certain actions, the same activity is associated with the service using

 bindService(new Intent(this, MyService.class), mConnection, Context.BIND_AUTO_CREATE); 

And when the action is destroyed, I call

 unbindService(mConnection); 

Previously, the service rebooted when I killed the same activity / application from the application tray and showed that the "running message service 1" was running under application control.

Now the service does not restart when the same action / application is destroyed.

And I get the message "0 process 1 working , which means that the service is actually not working.

The service does not restart when the application closes. My application consists of one action. Also, the service starts at startup after the system boots.

Why is the service process killed when I start it using startService () ??

change

The service used to restart earlier after I closed the application from the application tray. But now all of a sudden with the same code this is not the case. This happens with other applications when I close them. eg.

enter image description here

+25
android android-service
Dec 15 '13 at 7:46
source share
6 answers

Here is a workaround I came across and works well for restarting the service if its process was killed when the application was closed. In your service, add the following code.

I came across this workaround in this thread.

 @Override public void onTaskRemoved(Intent rootIntent){ Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass()); restartServiceIntent.setPackage(getPackageName()); PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT); AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); alarmService.set( AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePendingIntent); super.onTaskRemoved(rootIntent); } 

The error seems to be that the application process is killed. It makes no sense to start the service if its process is killed.

+43
Dec 26 '13 at 7:42
source share
β€” -

Keep in mind: onDestroy is not always called. You should not put the code this way.
When activity is forced shut down or shut down abnormally by the system, onDestroy does not receive the call.

+4
Sep 16 '15 at 8:15
source share

Unfortunately, this is a difficult issue due to how Android works. There are a number of strategies, each of which relates to different parts of the problem. Combine several strategies for best results.

Please note that some of these strategies are no longer needed in later versions of Android.

1. Run the action

What to do

Taken from the Foreground Service, killed when receiving a broadcast after acitivty waved off the task list :

In the foreground:

  @Override public void onTaskRemoved( Intent rootIntent ) { Intent intent = new Intent( this, DummyActivity.class ); intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK ); startActivity( intent ); } 

In the manifest:

  <activity android:name=".DummyActivity" android:theme="@android:style/Theme.NoDisplay" android:enabled="true" android:allowTaskReparenting="true" android:noHistory="true" android:excludeFromRecents="true" android:alwaysRetainTaskState="false" android:stateNotNeeded="true" android:clearTaskOnLaunch="true" android:finishOnTaskLaunch="true" /> 

(If your service is in a different process, set this activity process to the same.)

In DummyActivity.java:

  public class DummyActivity extends Activity { @Override public void onCreate( Bundle icicle ) { super.onCreate( icicle ); finish(); } } 

Side effects

Causes retentate activity to close. Normally disabling the application does not close the repetition activity.

disadvantages

It takes effect when fictitious activity begins , which can take half a second or more, so this still leaves the service open in order to be killed a bit.

Explanation

When you uninstall / delete your application, set the waitingToKill flag. While this flag is set, Android can kill the process at any time in the future , for example, when receiving a broadcast . The start of an action clears this flag.

2. Spam a BroadcastReceiver with Foreground BroadcastReceiver

What to do

Combine this in your utility code:

 if (Build.VERSION.SDK_INT >= 16) { Intent intent = new Intent(this, DummyReceiver.class); intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); //This seems to be timing-related; the more times we do this, //the less likely the process gets killed for (int i = 0; i < 50; ++i) sendBroadcast(intent); } 

Create a dummy broadcast receiver:

 public class DummyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {} } 

Add the recipient to your manifest:

 <receiver android:name=".DummyReceiver" /> 

Side effects

May cause a slight delay (~ 250 ms) / freeze when a task is removed from the repeat screen.

disadvantages

This only keeps working in the process of receiving broadcasts. the waitingToKill flag is still set , so after that the process can be killed, for example, when the broadcast is received.

Explanation

If your process does not work in foreground priority, Android will try to kill it immediately . Receiving broadcast broadcasts in the foreground temporarily prevents this, as a result of which the waitingToKill flag is set waitingToKill .

3. Do not get attached to services

Service binding seems to increase the likelihood that the service process will be destroyed immediately when the task is deleted.

+2
Nov 09 '16 at 20:59
source share

I know this question is old, but I recently ran into this problem, and suddenly my service stopped closing the application. It used to work fine. This problem has spent a lot of time. For others who have a similar problem, be sure to turn OFF YOUR DATA BACKGROUND. This was the problem I ran into, and it really makes sense, since when background data is limited by the background process, it will not execute.

0
Jun 20 '16 at 18:02
source share

onDestroy is not always called. The main problem in your case is ur that cannot start the service when the application is closed, this is the time and the Android OS (on some OSs ) will kill the service, if you cannot restart the service then call the alarm box to start the receiver like this ,

There is a manifesto

  <service android:name=".BackgroundService" android:description="@string/app_name" android:enabled="true" android:label="Notification" /> <receiver android:name="AlarmReceiver"> <intent-filter> <action android:name="REFRESH_THIS" /> </intent-filter> </receiver> 

IN The main activation trigger in this way

 String alarm = Context.ALARM_SERVICE; AlarmManager am = (AlarmManager) getSystemService(alarm); Intent intent = new Intent("REFRESH_THIS"); PendingIntent pi = PendingIntent.getBroadcast(this, 123456789, intent, 0); int type = AlarmManager.RTC_WAKEUP; long interval = 1000 * 50; am.setInexactRepeating(type, System.currentTimeMillis(), interval, pi); 

this will cause reciver and reciver,

 public class AlarmReceiver extends BroadcastReceiver { Context context; @Override public void onReceive(Context context, Intent intent) { this.context = context; System.out.println("Alarma Reciver Called"); if (isMyServiceRunning(this.context, BackgroundService.class)) { System.out.println("alredy running no need to start again"); } else { Intent background = new Intent(context, BackgroundService.class); context.startService(background); } } public static boolean isMyServiceRunning(Context context, Class<?> serviceClass) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE); if (services != null) { for (int i = 0; i < services.size(); i++) { if ((serviceClass.getName()).equals(services.get(i).service.getClassName()) && services.get(i).pid != 0) { return true; } } } return false; } } 

And this relaxer Alaram calls once when the Android application is open and when the application is closed. SO service is like this,

 public class BackgroundService extends Service { private String LOG_TAG = null; @Override public void onCreate() { super.onCreate(); LOG_TAG = "app_name"; Log.i(LOG_TAG, "service created"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(LOG_TAG, "In onStartCommand"); //ur actual code return START_STICKY; } @Override public IBinder onBind(Intent intent) { // Wont be called as service is not bound Log.i(LOG_TAG, "In onBind"); return null; } @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override public void onTaskRemoved(Intent rootIntent) { super.onTaskRemoved(rootIntent); Log.i(LOG_TAG, "In onTaskRemoved"); } @Override public void onDestroy() { super.onDestroy(); Log.i(LOG_TAG, "In onDestroyed"); } } 
0
Jan 16 '17 at 11:10
source share

when there is no binding to the service or well-established front space, the android system recognizes the service as an unused congestion service, which should be disabled. Here is the best way to maintain service, even if the application is closed: AlarmManager or Service

-one
Dec 15 '13 at 11:11
source share



All Articles