【问题标题】:Repeating Alarm notification in android在android中重复警报通知
【发布时间】:2017-07-10 08:26:11
【问题描述】:

这是我在活动类的 oncreate 中设置每日警报服务的代码。

  Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        int curHr = calendar.get(Calendar.HOUR_OF_DAY);
        if (curHr >=9) {
        // Since current hour is over 9, setting the date to the next day
            calendar.add(Calendar.DATE, 1);
             }

    calendar.set(Calendar.HOUR_OF_DAY, aHOUR);
    calendar.set(Calendar.MINUTE, aMINUTE);
    calendar.set(Calendar.SECOND, aSECOND);
    Intent intent1 = new Intent(getBaseContext(), MyReciever.class);
    intent1.putExtra("requestcode", "0");
    intent1.putExtra("status", "1");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), INTERVAL, pendingIntent);

这是我在接收方的代码

String req=intent.getStringExtra("requestcode");
        String state=intent.getStringExtra("status");

    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(context, FirstActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, Integer.valueOf(req),
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    if(Integer.valueOf(req)==1)
    {
        NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
                context).setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Hi "+appref.getUserFirstName()+" "+appref.getUserLastName())
                .setContentText("Having a great Day? Log it in MyApp").setSound(alarmSound)
                .setAutoCancel(true).setWhen(when)
                .setContentIntent(pendingIntent)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
        if(Integer.valueOf(state)==1)
        {

            notificationManager.notify(MID1, mNotifyBuilder.build());

        }
        else
        {
            notificationManager.cancel(MID1);

        }

除了重复,一切正常。问题是alarm notification 在第二天无法工作。如果您对此问题有任何解决方案,请提供帮助。

INTERVAL 是 alarmmanager.intervalday_ 相同的问题,然后更改为 int interval=86000L 但问题看起来相同。

提前致谢

【问题讨论】:

  • 设备是否在这段时间内启动?如果是这样,那么AlarmManager 请求将被丢弃。您可能已经这样做了,但请尝试将持续时间更改为更小(约 5 分钟),以确认没有其他干扰。
  • 5 分钟。 15 分钟……甚至几个小时都在工作。手动不重新启动我的系统。即使启动发生,它会影响接收者等级吗?
  • 是的...AlarmManager 请求在系统启动时终止。即使它现在没有发生,你也必须补偿它。设置一个BroadcastReceiver 用于监听启动BOOT_COMPLETED 事件,然后再次调用AlarmManager 方法。
  • 您能否调试并发现您的接收器是否在第二天被调用。如果您的接收器被调用,则问题可能会显示通知。 notificationManager.notify(MID1, mNotifyBuilder.build());在这个调用中确保 MIDI 是唯一的。如果您输入相同的 MID1 值,则通知与现有通知重叠并显示为一个。如果您的调度程序运行良好,这可能是原因。
  • Shaishav:我正在使用广播接收器,但未使用意图过滤器 BOOT_COMPLETED 并获得引导加载程序权限。它会导致问题吗?

标签: android alarmmanager repeatingalarm


【解决方案1】:

试试这个。它对我有用。

Intent myIntent = new Intent(this, Receiver.class);
myIntent.putExtra("key", "Alert");
pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar=Calendar.getInstance();
// Calendar.set(int year, int month, int day, int hourOfDay, int minute, int second)
calendar.set(2013, Calendar.OCTOBER, 10, 12, 10, 20);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);

这里的参数10*1000 = 10秒会重复报警

Receiver.java

public class Receiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
       Toast.makeText(context,intent.getStringExtra("key"),Toast.LENGTH_SHORT).show();
  }

}

【讨论】:

  • 它是单日的,对吗?我的要求是重复报警
  • 不,这将每隔 10 秒重复一次。对你来说,它应该每天重复然后你应该有这个1000 * 60 * 60 * 24 = 24 Hours
  • 谢谢兄弟。我会检查并回复ma反馈
猜你喜欢
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多