2015-05-18 27 views
1

我开发了一个应用程序,它会在特定时间提醒我。因此我实现了一个启动通知的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

+0

而在你看来,如果应用程序通过任务管理器关闭它仍然应该显示通知或..? – Divers

+0

你在任务管理器中终止应用程序,它会删除通知,因为你杀了应用程序。这正是应该发生的事情。 – DeeV

+0

好的。但我希望用户仍然可以收到通知。我该如何处理? – Bolic

回答

1

不应该担心关于用户通过任务管理器查杀应用程序。如果发生这种情况,你真的不应该自动启动备份。这不是一种用户友好的做事方式。

什么你应该担心是系统重启在这种情况下,设备开启时,你可以注册你的应用程序通知。

退房this回答有关开机时启动服务的更多详细信息。

也开始为您服务sticky

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