我正在通过服务添加警报,并且当我按下注销时,我还创建了一个广播以取消同一服务中的所有警报(待定意图)我想取消注册所有警报,所以我调用了那个广播接收器,但那个警报仍然存在射击。我也尝试了同样的事情,将待处理的 Intent 对象存储到数组列表中,然后使用相同的对象删除警报,这是我的代码..
public class AddReminderService extends Service {
ArrayList<Integer> intentArray;
ArrayList<pendingIntent> pendingIntentArray;
AlarmManager alarmManager,CancelalarmManager;
ArrayList<Bundle> bundleArray;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
registerReceiver(cancelAlarms, new IntentFilter("com.x.y.ReminderAlerts.AlarmCancel"));
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if (intent.hasExtra("ReminderDetails")) {
@SuppressWarnings("unchecked")
ArrayList<HashMap<String, String>> entries = (ArrayList<HashMap<String, String>>) intent
.getSerializableExtra("ReminderDetails");
Log.v("AddReminderService", "OnStart entries size()"+entries.size());
addReminder(entries);
}
}
private void addReminder(ArrayList<HashMap<String, String>> entries)
{
intentArray= new ArrayList<Integer>();
bundleArray= new ArrayList<Bundle>();
pendingIntentArray= new ArrayList<pendingIntent>();
for (int i = 0; i < entries.size(); i++) {
alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
setAlarm(i,alarmTime, activity, stringStartTime, stringEndTime, ReminderSysID);
}
}
private void setAlarm(int position,long alarmTime, String actvity, String startTime,
String endTime, String ReminderSysID) {
if (alarmTime > (new Date()).getTime()) {
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
intent.setAction("com.x.y.ReminderAlerts.ALARMRECIEVER");
intent.addCategory("com.x.y.ReminderAlerts.ALARMRECIEVER");
Bundle c = new Bundle();
c.putString("Actvity", actvity);
c.putString("Start_tim", startTime);
c.putString("Endtime", ReminderSysID);
c.putInt("ReminderSysID", Integer.parseInt(ReminderSysID));
intent.putExtras(c);
bundleArray.add(c);
PendingIntent pendingIntent = PendingIntent.getService(
getApplicationContext(), Integer.parseInt(ReminderSysID), intent,PendingIntent.FLAG_ONE_SHOT);
pendingIntentArray.add(pendingIntent);
intentArray.add(Integer.parseInt(ReminderSysID));
Log.i("intentArray alarm id",Integer.parseInt(ReminderSysID)+"" );
Log.e("registerReciever",""+pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
}
}
public BroadcastReceiver cancelAlarms = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("cancelalarmreciever","On alarm cancel reciever");
if(intentArray.size()>0){
for(int i=0; i<intentArray.size(); i++){
Log.i("cancelAlarms alarm id",intentArray.get(i)+"" );
Intent tempIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
intent.setAction("com.x.y.ReminderAlerts.ALARMRECIEVER");
intent.addCategory("com.x.y.ReminderAlerts.ALARMRECIEVER");
intent.putExtras(bundleArray.get(i));
PendingIntent pendingIntent = PendingIntent.getService(
getApplicationContext(), intentArray.get(i), tempIntent,PendingIntent.FLAG_UPDATE_CURRENT);
Log.v("registerReciever",""+pendingIntent);
///.cancel();
Log.v("cancelalarmreciever index","On alarm cancel reciever"+intentArray.get(i));
CancelalarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
CancelalarmManager.cancel(pendingIntent);
alarmManager.cancel(pendingIntentArray.get(i));
Log.v("cancelalarmreciever pendint intent","On alarm cancel reciever"+pendingIntentArray.get(i));
}
}
intentArray.clear();
pendingIntentArray.clear();
unregisterReceiver(cancelAlarms);
stopSelf();
}
};
}