alarmManager中有多个报警

时间:2016-06-05 19:08:47

标签: java android android-studio alarmmanager

这是一个短信计时器应用程序,所以我需要使用警报管理器设置多个警报,以便我可以向许多人发送消息。 我的代码是:

let prodConfig = require('./env/prod.js');
let devConfig = require('./env/dev.js');

let config;
if (process.env.NODE_ENV === 'production') {
  config = prodConfig;
}else {
  config = devConfig;
}

export default config;

onclick函数中的所有代码,因此当用户填写数据并单击发送时,应设置警报。 我最多只需要5个警报。 我尝试创建Phone = phone.getText().toString(); Message = message.getText().toString(); phone.setText(""); message.setText(""); //Validating if any field empty if (Phone.length() >= 10 && Message.length() > 0) { Toast.makeText(this.getApplicationContext(), "Your sms will be sent soon", Toast.LENGTH_SHORT).show(); //Getting Calender Reference Calendar cal = Calendar.getInstance(); int currentApiVersion = android.os.Build.VERSION.SDK_INT; if (currentApiVersion > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) { cal.set(Calendar.MINUTE, time_picker.getMinute()); cal.set(Calendar.HOUR_OF_DAY, time_picker.getHour()); } else { //Setting the date and time from the time picker cal.set(Calendar.MINUTE, time_picker.getCurrentMinute()); cal.set(Calendar.HOUR_OF_DAY, time_picker.getCurrentHour()); } int a = (Calendar.getInstance().get(Calendar.SECOND) * 1000); myIntent = new Intent(this, MyReceiver.class); //Pending Intent for sending the intent afterwards pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent, 0); alarmManager = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE)); alarmManager.set(AlarmManager.RTC, cal.getTimeInMillis() - a, pendingIntent); AlarmManager[],但它没有用,只有最新的闹钟被设置。

1 个答案:

答案 0 :(得分:0)

PendingIntents通常因其requestCode(像ID一样工作)而变得不同,这可能是导致您出现问题的原因,您可以像这样设置requestCode:

在onCreate之外添加:

static int i; 

并替换它:

pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, myIntent, 0);

用这个:

pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), i++ /*this number is the PendingIntent's requestCode*/, myIntent, 0);