In Android 4.4, Swiping App Out of Recent Tasks Permanently Kills Application with Its Service . Any Idea Why

In android 4.4, swiping app out of recent tasks permanently kills application with its service . Any idea why?

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);
}

clear Recent apps wipe the apps memory and my receiver stopped working

On some Huawei devices (and some LG and Xiaomi devices) you need to add your app to the list of apps that are allowed to run in the background. If you don't, once your app is stopped (by swiping from the recent tasks list, or by Android killing the app for resource reasons), it will NOT be automatically restarted.

On Huawei devices, the setting is called "protected apps". You cannot programmatically add your app to the list of "protected apps". You need to tell the user that he has to do it after you've installed your app. Well-known apps (like Whatsapp, Facebook, Google Mail) are automatically added by the manufacturer.

This behaviour may be different on different devices and it may be different on different versions of Android and it may be different if the device is "branded" for a specific mobile operator, as the mobile operators can also tinker with the settings themselves.

See "Protected Apps" setting on Huawei phones, and how to handle it for some more details.

EDIT: Added this:

Also, Android broke the "swipe from recents" behaviour in Android 4.4 (Kitkat) so that it causes problems for apps that have been swiped. Sticky services don't get retarted and broadcast Intents are not delivered. There is some information here about workarounds to deal with that: In android 4.4, swiping app out of recent tasks permanently kills application with its service . Any idea why?

Also, have you installed your app from the Google Play store? It is possible that the behaviour is different for apps that have been installed from the Play store versus apps that are locally installed (from downloads or via adb or whatever).

Prevent killing of service when clear recent tasks

For preventing service from killing i found one hack (actually i did read it somewhere here)

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

DummyActivity:

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

And that's it. But there's side effect of this hack - The panel with recents will be immediately hidden after you swipe app of

Android Bluetooth Binder is dead

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. Here you can find a "quick and dirty workaround":
In android 4.4, swiping app out of recent tasks permanently kills application with its service . Any idea why?

AsynchTask should not stop when I terminates Application

You need to explicitly call of asyncTaskObject.cancel(true); in onDestroy of your Activity(Or your event whenever you want.)

And then rewrite your while loop logic , something like that:

 // Create this variable is Your Asyantask class.
private boolean isRunning = true;

@Override
protected void onCancelled() {
isRunning = false;
}

@Override
protected String doInBackground(Void... params) {

while (isRunning ) {

// Your processing

}

return null;
}


Related Topics



Leave a reply



Submit