在android中重复警报通知

时间:2017-07-10 08:26:12

标签: android alarmmanager repeatingalarm

以下是我在活动课程的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,但问题看起来一样。

提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个。它对我有用。

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();
  }

}