繁体   English   中英

设置多个警报

[英]android Setting Multiple Alarms

首先,我已经阅读了许多有关此问题的答案,我想设置5个警报,但是在不同的时间,问题是最后一个优先于其他警报,所以我只能看到最后一个,我尝试更改请求PendingIntent的代码使其唯一,但问题仍然存在:我确实多次重写了代码,但结果相同,我试图为所有警报制作一种方法并更改请求代码,但没有用,我也尝试为具有唯一请求代码的每个警报使用不同的方法,但最后一个警报仍覆盖其他警报,这是我的两种方法的代码作为示例:
注意:我有fajrMillis,dhuhrMillis的变量:
是毫秒,已转换为日历以设置时间
fajrMessage,dhuhrMessage:要发送到接收器类中的通知的字符串消息
调用两个方法:

 setFajrAlarm(fajrMillis, 1, fajrMessage); setDhuhrAlarm(dhuhrMillis, 2, dhuhrMessage); 

 public void setFajrAlarm(long millis, int id, String message) { Calendar c = Calendar.getInstance(); c.setTimeInMillis(millis); PrayersReceiver.notificationId = id;// 1 PrayersReceiver.notificationMessage = message;// "حان الآن موعد صلاة الفجر" Intent intent = new Intent(getBaseContext(), PrayersReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),30000, pendingIntent); } public void setDhuhrAlarm(long millis, int id, String message) { Calendar c = Calendar.getInstance(); c.setTimeInMillis(millis); PrayersReceiver.notificationId = id;// 1 PrayersReceiver.notificationMessage = message;// "حان الآن موعد صلاة الظهر Intent intent = new Intent(getBaseContext(), PrayersReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 2, intent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 5000, pendingIntent); } //--------------------------------------------------------------------------------- 


接收器类

 package com.prayers; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.widget.Toast; public class PrayersReceiver extends BroadcastReceiver { public static String notificationMessage = ""; public static int notificationId ; @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Toast.makeText(context, "Time For Prayer", 3000).show(); // ----------------- // --------------Notification Part--------------------------- NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( context).setSmallIcon(R.drawable.ic_launcher) .setContentTitle("حان الآن موعد الصلاة") .setContentText(notificationMessage); NotificationManager mNotificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(notificationId, mBuilder.build()); } } 


在此先感谢您,希望尽快得到答案

我知道答案,所以如果有人遇到这个问题以获得解决方案:我正在设置接收器类中的变量值,因此最后一个方法调用将覆盖先前的调用:

 PrayersReceiver.notificationId = id; PrayersReceiver.notificationMessage = message; 

问题出在方法主体的这两行中:因此它可以通过以下方式解决:

 Intent intent = new Intent(getBaseContext(), PrayersReceiver.class); intent.putExtra("notificationId", id); intent.putExtra("notificationMessage", message); 

并在接收器类中获得两个值:

 Bundle bundle = intent.getExtras(); if (bundle == null) { Log.i("PrayersReceiver", "bundle is null"); return; } notificationId = bundle.getInt("notificationId"); notificationMessage = bundle.getString("notificationMessage"); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM