Unlike previous versions, in 4.4, swiping app out of recent tasks permanently kills app along with its service(like force-stop) even though it's running background services. It shows 0 processes 1 service but service also doesn't work. Ideally it shouldn't kill background service, and it doesn't in versions prior to 4.3. Any idea why is it happening in 4.4?
Asked
Active
Viewed 8,431 times
2 Answers
26
Got it. Its a bug in 4.4. I tried this and it worked perfectly fine(its a dirty workout though).
Just override this method -:
public void onTaskRemoved(Intent rootIntent) {
Log.e("FLAGX : ", ServiceInfo.FLAG_STOP_WITH_TASK + "");
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);
}

Vipul J
- 6,641
- 9
- 46
- 61
-
why are you using the AlarmManager here? Will I have to implement this on a regular service too ? – Rakeeb Rajbhandari Dec 19 '13 at 09:46
-
5I am using it because the moment you swipe, android kills everything, including your service and then there is no way to restart your service. By setting alarm, I am ensuring restart of service. – Vipul J Dec 19 '13 at 10:03
-
I upvoted ... but how can I accept the answer ? its your question :P – Rakeeb Rajbhandari Dec 19 '13 at 10:11
-
haha dude it's your question. only you have the power to accept :P – Rakeeb Rajbhandari Dec 19 '13 at 10:21
-
Its my question, thats why I can't accept it. :P Anyways! :-D – Vipul J Dec 19 '13 at 10:22
-
Deleting these comments, else people will spam it. :-) – Vipul J Dec 19 '13 at 10:23
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43501/discussion-between-vipul-j-and-user2247689) – Vipul J Dec 19 '13 at 10:24
-
@VipulJ you can accept your answer, but in two days from asking it. – Zoltán Dec 19 '13 at 10:33
-
@Vipul - What is the source of the information regarding the new 4.4 behavior of swiping aside recent apps? Did you read an official documentation or is it just personal experience? – Dev-iL Jun 28 '14 at 15:25
-
This is what I got to know after playing a lot. Didnt find anything that time. – Vipul J Jun 28 '14 at 15:30
4
From this issue Foreground service killed when receiving broadcast after acitivty swiped away in task list
Here is the solution
In the foreground service:
@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"
/>
In DummyActivity.java:
public class DummyActivity extends Activity {
@Override
public void onCreate( Bundle icicle ) {
super.onCreate( icicle );
finish();
}
}

kalyan pvs
- 14,486
- 4
- 41
- 59
-
1This is an solution for android 4.4, not for 5.1 though. My one device with android 6.0 doesn't have the bug so it seems. – Harmen Apr 20 '16 at 14:32