我正在通过服务添加警报,我还创建一个广播来取消所有警报(Pending Intents)在同一服务中当我按下注销时我想取消注册所有警报,所以即时通讯呼叫广播接收器,但仍然是警报仍在开火。我还尝试将待处理的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();
}
};
}
答案 0 :(得分:1)
你的代码需要很多爱: - (
无论如何,当您尝试取消onReceive()
中的闹钟时,请执行以下操作:
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));
你可能意味着在tempIntent
上设置ACTION,CATEGORY和extras,如下所示:
Intent tempIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
tempIntent .setAction("com.x.y.ReminderAlerts.ALARMRECIEVER");
tempIntent .addCategory("com.x.y.ReminderAlerts.ALARMRECIEVER");
tempIntent .putExtras(bundleArray.get(i));
答案 1 :(得分:0)
代码中有点错误。不要尝试使用相同的对象。创造一个不同的对象。