我开发了一个应用程序,它会在特定时间提醒我。因此我实现了一个启动通知的IntentService。问题是,通知将在应用程序处于前台或至少在后台打开时创建。当我通过任务管理器关闭应用程序时,通知不再运行。IntentService的通知
我使用错误的服务吗?或者我必须创建一些专业?
在intentservice类:
private static final String serviceName = "sebspr.de.deadlines.DeadLineService";
public DeadLineService() {
super(serviceName);
}
@Override
protected void onHandleIntent(Intent intent) {
Context ctx = getApplicationContext();
Intent intent = new Intent(ctx, DeadLineService.class);
PendingIntent pIntent = PendingIntent.getActivity(ctx, 0, intent, 0);
String title = getTitle(list.size());
String text = getText(list);
Notification noti = new Notification.Builder(ctx)
.setContentTitle(title)
.setContentText(text).setSmallIcon(R.mipmap.ic_notfi)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
INT在MainActivity:
Intent msgIntent = new Intent(this, DeadLineService.class);
startService(msgIntent);
UPDATE
考虑到我的实际问题的解决方案是,去看看在ArlarmManager。 IntentService在这里是错误的路。我发现了一个很好的教程在这里:AlarmManger Example
而在你看来,如果应用程序通过任务管理器关闭它仍然应该显示通知或..? – Divers
你在任务管理器中终止应用程序,它会删除通知,因为你杀了应用程序。这正是应该发生的事情。 – DeeV
好的。但我希望用户仍然可以收到通知。我该如何处理? – Bolic