从最近的应用程序中刷出应用程序后,服务代码不是 运行

Service code not running after application swiped from recent apps

您好,我正在使用 IntentService 根据存储在 notificationCalendar 变量中的时间设置向用户显示通知。我使用 AlarmManager 开始服务,当应用程序处于后台(按下主页按钮)或 运行 我收到通知但是当应用程序从最近的应用程序中滑动时没有收到通知。

我在网上搜索了不允许服务破坏的解决方案,所以我在服务 onStartCaommand 中 return START_STICKY 并且在清单中添加了 stopWithTask=false

我正在 MI phone 上测试 API 级别 22。

这是我的代码:

private void sendNotification(){
    Intent intent = new Intent(this, NotificationService.class);
    PendingIntent alarmIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, notificationCalendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, alarmIntent);
}

显示通知的服务class:

public class NotificationService extends Service {

private static final int NOTIFICATION_ID = 1;

public NotificationService() {
    super();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    processStartNotification();
    return START_STICKY;
}

@Override 
public IBinder onBind(Intent intent) {
    return null;
} 

private void processStartNotification() {
    // Do something. For example, fetch fresh data from backend to create a rich notification?
    NotificationManager notificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Habit Time")
            .setContentText("Hey time for your habit")
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setSound(alarmSound)
            .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});

    notificationManager.notify(NOTIFICATION_ID, mNotifyBuilder.build());
}

@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() + 1,
            restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
  }
}

//清单文件

<service
        android:name=".notification.NotificationService"
        android:enabled="true"
        android:stopWithTask="false"/>

在某些手机上,您需要将您的应用添加到允许 运行 在后台运行的应用列表中。部分华为、LG、小米手机有此功能。如果您的应用不在此列表中,从最近任务列表中滑动该任务将终止所有后台进程并且不会重新启动它们。

在设置->电池->管理应用电池使用中查看