2

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!

Aksiom
  • 1,565
  • 4
  • 25
  • 39
  • I'm not sure storing pending intents in shared preferences is even feasible. May be it is better to restate the question on the higher level: what is it you need to store (in shared prefernces or elsewhere) which you want to use as pending intent? – amoiseyev Sep 09 '13 at 02:06
  • you cant store PendingIntent or array of such objects to SharedPreferences. – MikeL Mar 26 '15 at 16:33

3 Answers3

0

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 its toUri() method that returns String)

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).

Stanislav Agapov
  • 947
  • 8
  • 15
0

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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
-2

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.

Community
  • 1
  • 1
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45