sac*_*chi 1 android alarmmanager
我正面临取消特定警报的问题.我使用报警管理器SET方法设置多个报警.但我无法取消特定的警报.如果我取消,它将取消之前设置的所有警报.我正在使用单个ALARM MANAGER ID来设置多个警报.
保存方法
private void saveState()
{
// String date = mDateText.getText().toString();
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
String sdate = sDateText.getText().toString();
String stime = sTimeText.getText().toString();
String rdate = rDateText.getText().toString();
String rtime = rTimeText.getText().toString();
String appRemDate = ((Button)findViewById(R.id.enterdaterem)).getText().toString();
String appRemTime = ((Button)findViewById(R.id.entertimerem)).getText().toString();
Calendar cal = Calendar.getInstance();
//setting date & month
if(appRemDate.substring(0,2).endsWith("-" ) && appRemDate.substring(2,4).endsWith("-" ))
{
cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(4,8)));
cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(2,3)) -1);
cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,1)));
}
else if( !appRemDate.substring(0,2).contains("-" ) && !appRemDate.substring(3,5).contains("-" ) )
{
cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(6,10)));
cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(3,5)) -1);
cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,2)));
}
else if( appRemDate.substring(0,2).endsWith("-" ) && !appRemDate.substring(2,4).endsWith("-" ))
{
cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(5,9)));
cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(2,4)) -1);
cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,1)));
}
else
{
cal.set(Calendar.YEAR, Integer.parseInt(appRemDate.substring(5,9)));
cal.set(Calendar.MONTH, Integer.parseInt(appRemDate.substring(3,4)) -1);
cal.set(Calendar.DATE, Integer.parseInt(appRemDate.substring(0,2)));
}
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(appRemTime.substring(0, 2)));
cal.set(Calendar.MINUTE, Integer.parseInt(appRemTime.substring(3, 5)));
cal.set(Calendar.SECOND, 0);
if (mRowId == null)
{
long id = mDbHelper.createNote( title, body, sdate, stime, rdate, rtime);
invokeAlaram( cal.getTimeInMillis(), id );
if (id > 0)
{
mRowId = id;
}
}
else
{
long id = mDbHelper.updateNote(mRowId, title, body, sdate, stime, rdate, rtime);
invokeAlaram( cal.getTimeInMillis(), id );
}
}
Run Code Online (Sandbox Code Playgroud)
反馈报警方法
private void invokeAlaram(long invokeTime, long id )
{
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, MyAlarmServiceForAppointments.class);
i.putExtra("rowId", String.valueOf(id));
am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(this , (int) System.currentTimeMillis() ,i , 0));
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我如何取消特定的警报.
Pra*_*tik 10
当您将请求代码的系统毫秒时间传递到getService()时,就像这里一样
am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(this , (int) System.currentTimeMillis() ,i , 0));
Run Code Online (Sandbox Code Playgroud)
而不是使用系统时间,你必须传递你的唯一号码,考虑到特定意图的请求是不同的.
现在你必须保留这个基于此的所有请求代码,你可以取消警报
注意:作为Intent对象,您传递给PendingIntent,它必须是相同的,用于取消您在设定时间定义的警报
am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(this , 1 ,i , 0));
am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(this , 2 ,i , 0));
Run Code Online (Sandbox Code Playgroud)
等等
现在取消
am.cancel(PendingIntent.getService(this , 2 ,i , 0));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2811 次 |
| 最近记录: |