2013-03-01 102 views
0

我正在创建一个调度程序应用程序。在此我使用AlarmManagerPendingIntent设置了多个事件。我用下面的方法:使用PendingIntent的多个警报

在这里我声明PendingIntent阵列:

public static ArrayList <PendingIntent> intentArray= new ArrayList <PendingIntent>(); 

在这里,我将意图到数组:

PendingIntent pendingIntent = PendingIntent.getBroadcast(
     EditScheduleActivity.this, intentid, notifyintent, 
     PendingIntent.FLAG_UPDATE_CURRENT); //intent id is unique 

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
     pendingIntent); 
MainActivity.intentArray.add(pendingIntent); 

我在这里清除报警:

EditScheduleActivity.alarmManager.cancel(intentArray.get(selecteditemid_int)); 
intentArray.remove(selecteditemid_int); 

但是,当我清除报警,我得到一个ArrayIndexOutOfBoundsException,我认为问题是当我重新启动应用程序时,意图数组列表被重新初始化并抛出异常。我如何通过保持意图数组列表不被重新初始化来克服这个问题?

+0

SharePrefernece ..! – RobinHood 2013-03-01 05:12:20

+0

但是使用sharedpreference可以存储意向数组吗? – 2013-03-01 05:14:05

回答

-1

这与Android没有什么关系。一个ArrayOutOfBoundsException意味着,当你做:

intentArray.remove(selecteditemid_int); 

selecteditemid_int比阵列的长度。

+0

是的,这就是我所说的。当我重新启动应用程序时,Intent数组被重新初始化。数组列表被清除。这就是为什么我得到的数组超出了绑定的异常。我需要保持意图数组不被重新初始化。我该怎么做? – 2013-03-01 05:15:43

0

我认为问题是当我重新启动应用程序意图数组列表获取重新初始化并抛出异常。

在你的情况,你必须使用Sharepreference or database,商店的ArrayList到shareprence同时intentArray.remove(selecteditemid_int);更新您的SP,那么下一次它会给你上次存储的值,请检查下面的链接。

Using preference API in Android applications

Stackoverflow Answer