从最近的应用程序刷新应用程序后服务代码未运行

时间:2016-12-22 08:11:31

标签: android android-service android-notifications

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

我在网上搜索了不允许服务销毁的解决方案,因此我在服务START_STICKY中返回onStartCaommand,并在清单中添加了stopWithTask=false

我正在使用API​​等级为22的MI手机上进行测试。

这是我的代码:

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

显示通知的服务类:

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"/>

1 个答案:

答案 0 :(得分:1)

在某些手机上,您需要将应用添加到允许在后台运行的应用列表中。一些华为,LG和小米的手机都有这个。如果您的应用不在此列表中,则从最近任务列表中滑动任务将终止所有后台进程,而不会重新启动它们。

查看设置 - &gt;电池 - &gt;管理应用电池使用情况