首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法控制通知管理器android

无法控制通知管理器android
EN

Stack Overflow用户
提问于 2015-10-24 21:09:34
回答 1查看 272关注 0票数 0

我有一个application,应该每天准确的时间发送notifications。可以随时触发,但每次关闭应用程序notification时,它都不会出现。

通知类别:

代码语言:javascript
复制
 @Override
public int onStartCommand(Intent intent,int flags, int startId)
{
    super.onStartCommand(intent, flags, startId);
    Context context = getApplicationContext();
    Intent resultIntent = new Intent(getApplicationContext(), Love.class);

    PendingIntent contentIntent = PendingIntent.getActivity(context,
            0, resultIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    /* Invoking the default notification service */
    NotificationCompat.Builder  mBuilder = new NotificationCompat.Builder(context);

    mBuilder.setContentTitle("New Message");
    mBuilder.setContentText("You've received new message.");
    mBuilder.setTicker("New Message Alert!");
    mBuilder.setSmallIcon(R.mipmap.ic_launcher);
    mBuilder.setContentIntent(contentIntent);

    android.app.Notification notification = mBuilder.build();
    notification.defaults = notification.DEFAULT_SOUND |
            notification.DEFAULT_VIBRATE;
    notification.flags |= notification.FLAG_AUTO_CANCEL;

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);

    return Service.START_STICKY;
}

MainActivity:

代码语言:javascript
复制
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, 10);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    long day = TimeUnit.HOURS.toMillis(24);


    Intent alarmIntent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),day, pendingIntent);

和BroadcastReceiver:

公共类AlarmReceiver扩展BroadcastReceiver {

代码语言:javascript
复制
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent service1 = new Intent(context, Notification.class);
        context.startService(service1);

    }
}

接受者:

代码语言:javascript
复制
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent service1 = new Intent(context, Notification.class);
        context.startService(service1);

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        Boolean notif = sharedPreferences.getBoolean("notif", true);
        if (!notif) context.stopService(service1);

    }

Notification只能在10:00发送,但是它有非常奇怪的行为,而且stopService并不是每次都能工作(即使是假的)。我想这是用旗子做的,但我仍然不知道我到底做错了什么。

提前谢谢。而且,请不要低估我的问题,即使这是非常容易或只是愚蠢的。我是个新手,只是想知道。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-23 15:38:52

你只需要摧毁服务,当它完成时:

代码语言:javascript
复制
NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
stopSelf()
return Service.START_STICKY;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33323382

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档