Android AlarmManager problem
I'm a new developer android application.I've problem about AlarmManager,I using AlarmManager to set alert for my application.
I was set time to alert 2 time but my application was alert a one time
example,I'll set alarm at 15.30 and 16.30,My application will alert at 16.30 but not alert at 15.30.I've no idea to fix this problem,I hope someone please tell me to fix this problem
This my code
confirmButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
alarm();
finish();
}
});
This alarm method,I'll get date and time from button and use it to set calendar
private void alarm(){
final String day = mBtnselectDate.getText().toString();
final String time = mBtnselect开发者_JS百科Time.getText().toString();
y = day.substring(0, 4);
m = day.substring(5, 7);
d = day.substring(8);
ALARM_HOUR = time.substring(0, 2);
ALARM_MINUTE = time.substring(3);
year = Integer.parseInt(y);
month = Integer.parseInt(m);
mon = month - 1;
date = Integer.parseInt(d);
Hour = Integer.parseInt(ALARM_HOUR);
Min = Integer.parseInt(ALARM_MINUTE);
Intent AlarmIntent = new Intent(Organaizer2.this, AlarmReceiver2.class);
AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent Sender = PendingIntent.getBroadcast(Organaizer2.this, 0, AlarmIntent, 0);
Calendar alarmCalendar = Calendar.getInstance();
alarmCalendar.set(Calendar.HOUR_OF_DAY, Hour);
alarmCalendar.set(Calendar.MINUTE, Min);
alarmCalendar.set(Calendar.YEAR, year);
alarmCalendar.set(Calendar.MONTH, mon);
alarmCalendar.set(Calendar.DATE, date);
AlmMgr.set(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), Sender);
}
if you see problem please tell me,Thank you,Arles
Your second alarm is replacing the first alarm, because both use equivalent Intent
objects. Add a call to setAction()
on your AlarmIntent
(BTW, recommended Java style is lowercase first letters for variables) to set a unique action string. You can also try providing a different request code (the first 0
in your call to getBroadcast()
), though that parameter is not well documented and I am not sure if it will give you distinct alarms or not.
精彩评论