我希望从用户每隔5分钟发送给用户的重复通知,只要用户不接受它们。而他什么时候我想取消闹钟。
这是我的代码,但我不知道如何处理警报管理器和日历:
acceptBtnPendingIntent = PendingIntent.getBroadcast(this, 123, acceptBtnIntent, 0);
remoteView = new RemoteViews(this.getPackageName(), R.layout.notification_layout);
remoteView.setOnClickPendingIntent(R.id.acceptCallBtn, acceptBtnPendingIntent);
remoteView.setImageViewResource(R.id.notificationIcon, R.drawable.notification_icon);
remoteView.setTextViewText(R.id.fromTxt, fromId);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000 * 60 * 20, acceptBtnPendingIntent);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.mipmap.ic_launcher)
.setCustomHeadsUpContentView(remoteView)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(acceptBtnPendingIntent)
.setAutoCancel(true)
.setPriority(NotificationManager.IMPORTANCE_HIGH);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationsId, mBuilder.build());
希望这可以适用于您的情况。
//write the code where you would like to create and call alarm repetitively
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Intent intent = new Intent(context,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1234/*unique request code*/, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),5 * 60 * 1000 /*5 min in millis*/, pendingintent);
//your Broadcast Receiver
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
//Acquire the lock
wl.acquire();
showNotification(context);
if (intent.getAction().equals("USER_ACCEPTED")) {
//TODO: do your action you would like to do: do cancellation of alarm.. code here!
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context,AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1234/*unique request code*/, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.cancel(pendingIntent);
pendingIntent.cancel();
}
}
public void showNotification(){
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.mipmap.ic_launcher)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setPriority(NotificationManager.IMPORTANCE_HIGH);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationsId, mBuilder.build());
}
我已经给出了可以帮助你的代码。让我知道我是不是错了。