我正在进行闹钟应用,用户可以使用不同的响度和铃声添加多个闹钟。在广播接收器我想得到id(我在alarmpref活动设置id:
public void setAlarm(Calendar calendar){
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), PendingIntent.FLAG_ONE_SHOT, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 86400000, pendingIntent);
Intent i = new Intent(this, AlarmReceiver.class);
i.putExtra("id", 1);
startActivity(i);
}
并使用它播放选定的铃声和响亮。谢谢
答案 0 :(得分:-1)
您需要按如下方式向Intent对象添加Extras:
public void setAlarm(Calendar calendar){
int requestCode=100;
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
intent.putExtra("someid",123);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 86400000, pendingIntent);
}
并且也不需要写作:
Intent i = new Intent(this, AlarmReceiver.class);
i.putExtra("id", 1);
startActivity(i);
当达到警报的时间时,会自动启动AlarmReceiver。