通知的警报管理器无法正常工作

时间:2018-08-03 05:53:17

标签: android alarmmanager

我已经在Android中使用警报管理器来显示一些通知,但是它无法正常工作。

我的活动代码是:

Intent i = new Intent(this, AlarmBroadcastReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);
        AlarmManager alarmManager = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);

        alarmManager.cancel(pendingIntent);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (int) (10*60 * 1000),
                pendingIntent);

15分钟后接收通知的代码是:

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
        PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);
        am.cancel(pi);
        am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() +
                        (int) ( 5*60 * 1000), pi);

1 个答案:

答案 0 :(得分:1)

使用服务时使用这种类型的AlarmManager,而不需要更改由getBroadCast插入的getService。有时接收方无法通话,并且在后台运行应用程序时也会在某些设备中造成问题

   Intent intent = new Intent(myContext, SetAlarmService.class);
            intent.putExtra("salah",i);
            intent.putExtra("repeat",HowManyTimeRepeatAlarm);
            PendingIntent pendingIntent = PendingIntent.getService(
                    myContext, i, intent, 0);
            AlarmManager alarmManager = (AlarmManager) myContext.getSystemService(ALARM_SERVICE);
           // alarmManager.cancel(pendingIntent);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, reset_cal.getTimeInMillis(),60000 * 2 ,
                    pendingIntent);