如何在android kitkat中保持服务活着?

时间:2014-06-06 06:41:27

标签: android android-4.4-kitkat

朋友们,我已经搜索了很多但没有找到Google所做的服务变更,我需要保持我的服务。

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

    return START_STICKY;
    } 

但它没有在android 4.4.2中重新启动该服务吗?

1 个答案:

答案 0 :(得分:2)

您可以将它用于Android kitkat,因为它们正在从后台清除进程。

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

        }