我设法理解了这段代码。此代码用于提醒应用程序。此代码的作用是设置和取消警报。但是,我不知道新创建的引用如何取消现有的PendingIntent。请解释一下这段代码(执行方法)。
以下是代码:
public class AlarmService extends IntentService {
private static final String TAG = "AlarmService";
public static final String POPULATE = "POPULATE";
public static final String CREATE = "CREATE";
public static final String CANCEL = "CANCEL";
private IntentFilter matcher;
public AlarmService() {
super(TAG);
matcher = new IntentFilter();
matcher.addAction(POPULATE);
matcher.addAction(CREATE);
matcher.addAction(CANCEL);
}
@Override
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
String alarmId = intent.getStringExtra(AlarmMsg.COL_ALARMID);
String alarmMsgId = intent.getStringExtra(AlarmMsg.COL_ID);
String startTime = intent.getStringExtra(Alarm.COL_FROMDATE);
String endTime = intent.getStringExtra(Alarm.COL_TODATE);
if (matcher.matchAction(action)) {
if (POPULATE.equals(action)) {
RemindMe.dbHelper.populate(Long.parseLong(alarmId), RemindMe.db);
execute(CREATE, alarmId);
}
if (CREATE.equals(action)) {
execute(CREATE, alarmId, alarmMsgId, startTime, endTime);
}
if (CANCEL.equals(action)) {
execute(CANCEL, alarmId, alarmMsgId, startTime, endTime);
RemindMe.dbHelper.shred(RemindMe.db);
}
}
}
/**
* @param action
* @param args {alarmId, alarmMsgId, startTime, endTime}
*/
private void execute(String action, String... args) {
Intent i;
PendingIntent pi;
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Cursor c;
String alarmId = (args!=null && args.length>0) ? args[0] : null;
String alarmMsgId = (args!=null && args.length>1) ? args[1] : null;
String startTime = (args!=null && args.length>2) ? args[2] : null;
String endTime = (args!=null && args.length>3) ? args[3] : null;
String status = CANCEL.equals(action) ? AlarmMsg.CANCELLED : AlarmMsg.ACTIVE;
if (alarmMsgId != null) {
c = RemindMe.db.query(AlarmMsg.TABLE_NAME, null, AlarmMsg.COL_ID+" = ?", new String[]{alarmMsgId}, null, null, null);
} else {
c = AlarmMsg.list(RemindMe.db, alarmId, startTime, endTime, status);
}
if (c.moveToFirst()) {
long now = System.currentTimeMillis();
long time, diff;
// long count = c.getCount();
// double MAX = alarmId!=null && count>30 ? Util.MONTH : Util.YEAR;
do {
i = new Intent(this, AlarmReceiver.class);
i.putExtra(AlarmMsg.COL_ID, c.getLong(c.getColumnIndex(AlarmMsg.COL_ID)));
i.putExtra(AlarmMsg.COL_ALARMID, c.getLong(c.getColumnIndex(AlarmMsg.COL_ALARMID)));
pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
// pi = PendingIntent.getService(context, requestCode, intent, flags);
time = c.getLong(c.getColumnIndex(AlarmMsg.COL_DATETIME));
diff = time-now + (long)Util.MIN;
if (CREATE.equals(action)) {
if (diff > 0 && diff < Util.YEAR)
am.set(AlarmManager.RTC_WAKEUP, time, pi);
//am.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, interval, operation);
} else if (CANCEL.equals(action)) {
am.cancel(pi);
}
} while(c.moveToNext());
}
c.close();
}
}
答案 0 :(得分:1)
Alarm Manager是一个SystemService。您可以从任何上下文访问它,如果您有启动警报的待处理意图。您可以从pendingIntent中取消它。 只需看看文档 Link to Document
希望它有所帮助。
答案 1 :(得分:0)
此代码取消了这样的警报:
} else if (CANCEL.equals(action)) {
am.cancel(pi);
这不会取消PendingIntent
,只会取消闹钟。
此代码永远不会设置多个警报,因为设置第二个警报的尝试将自动取消第一个警报。这是因为每次都使用相同的PendingIntent
。