问题
II couldn't find an answer for my problem so I decided to post a question. The question is simple.
How can I store an ArrayList<PendingIntent> into SharedPreferences? What is the best way, do i have to use some kind of Serialization?
Thank you in advance, any advice would be great!
回答1:
PendingIntent is created with getActivity(), getBroadcast() or getService(). Each of these functions has the same list of parameters:
(Context context, int requestCode, Intent intent, @Flags int flags)
You cannot save Context, but you can save:
- Type of 
PendingIntent(i.e."Activity","Broadcast"or"Service") - Request code and flags (integers)
 Intent(using itstoUri()method that returnsString)
Thus, you can save in SharedPreferences all data that represents your PendingIntent, excluding Context (but Context instance is always available in your application.). Then you can easily restore saved PendingIntent from this data (using parseUri() method for restoring Intent).
回答2:
PendingIntent implements Parcelable. You'll need to loop through your ArrayList and convert each PendingIntent into a String. You can then concatenate all the individual Strings into a single String (with some delimiter in between each one). Then write the resulting String to SharedPreferences. 
You can convert a Parcelable into a byte[] like this:
PendingIntent pendingIntent = // Your PendingIntent here
Parcel parcel = Parcel.obtain(); // Get a Parcel from the pool
pendingIntent.writeToParcel(parcel, 0);
byte[] bytes = parcel.marshall();
parcel.recycle(); // Return the Parcel to the pool
Now convert the byte[] to a String by using base64 encoding (or any other mechanism, like converting each byte into 2 hexadecimal digits). To use base64 you can do this:
String base64String = Base64.encodeToString(bytes, Base64.NO_WRAP | Base64.NO_PADDING);
See How to marshall and unmarshall a Parcelable to a byte array with help of Parcel? for more information about how to convert a Parcelable to byte[] and vice-versa.
回答3:
From AndroidSolution4u
//suppos you have already an arraylist like this
ArrayList<String> myArrayList=new ArrayList<String>();
myArrayList.add("value1");
myArrayList.add("value2");
myArrayList.add("value3");
myArrayList.add("value4");
myArrayList.add("value5");
myArrayList.add("value6");
Store arraylist in sharedpreference
SharedPreference sPrefs=PreferenceManager.getDefaultSharedPreferences(context);
SharedPreference.Editor sEdit=sPrefs.edit();
for(int i=0;i<myArrayList.size();i++)
{
     sEdit.putString("val"+i,myArrayList.get(i);
}
sEdit.putInt("size",myArrayList.size());
sEdit.commit();
Retrive arraylist from sharedpreference
I am retriving values in another arraylist
 ArrayList<String> myAList=new ArrayList<String>();
 int size=sPrefs.getInt("size",0);
 for(int j=0;j<size;j++)
 {
      myAList.add(sPrefs.getString("val"+j));
 }
EDIT
I am not sure but this may help you.
来源:https://stackoverflow.com/questions/18672035/store-arraylistpendingintent-into-sharedpreferences