【发布时间】:2016-12-22 23:30:37
【问题描述】:
您好,我正在使用IntentService 根据存储在notificationCalendar 变量中的时间设置向用户显示通知。我使用AlarmManager 启动服务,当应用程序处于后台(按下主页按钮)或运行时,我会收到通知,但是当从最近的应用程序中滑动应用程序时,不会收到通知。
我在网上搜索了不允许服务销毁的解决方案,因此我在服务onStartCaommand 中返回START_STICKY,并在清单中添加了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"/>
【问题讨论】:
-
您不能在
IntentService中覆盖onStartCommand()!如果你想要一个粘性服务,那么你需要扩展Service,而不是IntentService。 -
@DavidWasser 我将其更改为服务但仍然无法正常工作
-
请发布您修改后的代码(用您当前的实现更新帖子)并详细解释什么“不起作用”。
-
@david Wasser 我刚刚将意图服务更改为服务,问题是.....每当用户创建任何习惯并设置其时间时,我都会调用 sendNotification() 方法,但这些通知仅显示何时应用程序正在运行或在后台(按下主页按钮),但如果使用最近的应用程序按钮清除应用程序,则不会出现通知
-
在不同版本的 Android 和不同的手机制造商中有很多关于这种行为的投诉。试试这个:将
android:process=":remote"添加到清单中的<service>标记中,看看是否有帮助。这会将Service启动到单独的操作系统进程中。
标签: android android-service android-notifications