Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...