此代码工作正常但我想安排时间。我从Web服务特定时间获得了一个值。我必须在阵列中通过时间进行报警设置。但它没有用。只有一次通过意味着它工作正常。请给我解决方案。我想将它的数组值传递给报警管理器。
private void setAlarm(Calendar calSet) {
int hours = calSet.get(Calendar.HOUR);
int mnts = calSet.get(Calendar.MINUTE);
int ampm = calSet.get(Calendar.AM_PM);
String str = "AM";
if (ampm == 0) {
str = "AM";
} else {
str = "PM";
}
int i = 0;
String time = hours + " : " + mnts + " : " + str;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(ALARM_ACTION_NAME);
// Set the Alarm ID as extra data to be displayed in the popup
alarmIntent.putExtra("AlarmID", m_alarmId);
alarmIntent.putExtra("AlarmTime", time);
// Create the corresponding PendingIntent object
PendingIntent alarmPI = PendingIntent.getBroadcast(MainActivity.this, m_alarmId,
alarmIntent, 0);
// Register the alarm with the alarm manager
alarmManager.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
alarmPI);
}
AlarmServiceReceiverClass
public class AlarmServiceReceiver extends BroadcastReceiver {
// The alarm action defined in AndroidManifest.xml
private static final String ALARM_ACTION_NAME = "com.goodmor.broadcast.ALARM";
@Override
public void onReceive(Context context, Intent intent) {
// Toast.makeText(context , "Alarm received!",
// Toast.LENGTH_LONG).show();
// Handle the alarm broadcast
Log.i(this.toString(), "111111111111");
if (ALARM_ACTION_NAME.equals(intent.getAction())) {
// Launch the alarm popup dialog
Intent alarmIntent = new Intent("android.intent.action.MAIN");
alarmIntent.setClass(context, AndroidAlarmService.class);
alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Pass on the alarm ID as extra data
alarmIntent.putExtra("AlarmID", intent.getIntExtra("AlarmID", -1));
alarmIntent.putExtra("AlarmTime",
intent.getStringExtra("AlarmTime"));
// Start the popup activity
context.startActivity(alarmIntent);
}
}
}
答案 0 :(得分:0)
如果要添加另一个警报,则更改m_alarmId;如果不更改m_alarmId,则先前的警报将替换为新警报,之前的警报不会调用
PendingIntent alarmPI = PendingIntent.getBroadcast(MainActivity.this, **m_alarmId**,
alarmIntent, 0);
答案 1 :(得分:0)
//Edited
int m_alarmId=0;
private void setAlarm(Calendar calSet) {
int hours = calSet.get(Calendar.HOUR);
int mnts = calSet.get(Calendar.MINUTE);
int ampm = calSet.get(Calendar.AM_PM);
String str = "AM";
if (ampm == 0) {
str = "AM";
} else {
str = "PM";
}
int i = 0;
//Edited
m_alarmId++;
String time = hours + " : " + mnts + " : " + str;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(ALARM_ACTION_NAME);
// Set the Alarm ID as extra data to be displayed in the popup
alarmIntent.putExtra("AlarmID", m_alarmId);
alarmIntent.putExtra("AlarmTime", time);
// Create the corresponding PendingIntent object
PendingIntent alarmPI = PendingIntent.getBroadcast(MainActivity.this, m_alarmId,
alarmIntent, 0);
// Register the alarm with the alarm manager
alarmManager.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
alarmPI);
}
在这里你将警报ID传递给AlarmReceiver Class吧?然后在AlarmReceiver类中传递此警报ID,如果无法获取它,请发布您的AlarmReceiver类
在这里,您将m_alarmId传递给AlarmReceiver类,
alarmIntent.putExtra("AlarmID", m_alarmId);