【问题标题】:Why my regular notifications are stopped after my phone is getting off?为什么我的手机关机后我的常规通知会停止?
【发布时间】:2014-02-09 15:42:17
【问题描述】:

我编写了一个应用程序并设置了一个警报管理器,该管理器设置为每 3 小时发送一次通知。假设我的通知必须在 11:10 发送,而我的手机在 11:00 下车。所以,我不会收到任何通知。当我的手机打开时,我会在 2:10 收到下一个通知,所以一切正常。

不过,据观察,我的手机在两轮通知下机后不会收到任何通知。你有什么建议吗?

提供代码:

Intent intentAlarm = new Intent(this, NotifyBroadcast.class);
                      PendingIntent pintentAlarm = PendingIntent.getBroadcast(this, 0, intentAlarm, PendingIntent.FLAG_CANCEL_CURRENT);
                      AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                      // Start every 30 seconds
                      mgr.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), 300, pintentAlarm);


// NotifyBroadcast:


public  class NotifyBroadcast extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub




    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


            Intent resultIntent = new Intent(context, MainActivity.class);


            PendingIntent resultPendingIntent =
                    PendingIntent.getActivity(
                    context,
                    0,
                    resultIntent,
                    0
                ); 



            Notification notification = new Notification(R.drawable.ic_launcher, "Let me know what is your emotion buddy!", System.currentTimeMillis());
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.sound = Uri.parse("file:///sdcard/notification/notification.mp3");
            //notification.flags = Notification.FLAG_AUTO_CANCEL;

            notification.setLatestEventInfo(context, "emotion interface", "Let me know what is your emotion buddy!", resultPendingIntent);



            int mId = 001;
            // mId allows you to update the notification later on.
            mNotificationManager.notify(mId, notification); 
          //  mNotificationManager.notify(mId,mBuilder.build());

            // mNotificationManager.cancel(mId);

}

}

【问题讨论】:

  • 您能澄清一下问题所在吗?您的意思是在您的示例中,您的手机不仅错过了 11:10 通知,还错过了 2:10 通知,您想知道原因吗?

标签: java android alarmmanager


【解决方案1】:

下面的部分我不太明白:

I will not receive any notification after my phone is getting off for two round of notification

只需检查您是否在重新启动后重新启动服务。为此,您需要添加一个接收器,在重新启动后再次启动您的服务。您需要在 android.intent.action.BOOT_COMPLETED 意图广播上处理它。一些可能对您有帮助的问题是:

Android: make notification persist across phone reboot

android notification after reboot

Android: why did the Alarm notification stop after system reboot

希望这在某种程度上有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 2020-04-21
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    • 2020-01-31
    相关资源
    最近更新 更多