Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Intent to String and vice versa

i have an intent to start a shortcut-activity like this:

startActivity((Intent) shortcut_intent.getExtras().get(Intent.EXTRA_SHORTCUT_INTENT));

i need to convert the shortcut_intent to a string to save it in sqlite db. I tried so much for so long with no success. currently i`m standing here:

convert intent to string:

String uri_string = mIntent_shortcut_intent.toUri(0);

create new intent:

Intent intent = new Intent();

and parse extras from uri:

intent.putExtra("android.intent.extra.shortcut.INTENT", Uri.parse(uri_string));

not working/app crashing sadly ;(

can someone help me with this? or tell me an alternative to save an intent persistent in sqlite db?

thx in advance


UPDATE:

as pskink suggested to parcel, unmarshall the extras-bundle and vice versa i did the following:

Bundle bundle=shortcutIntent.getExtras();
        Parcel parcel=Parcel.obtain();
        bundle.writeToParcel(parcel, 0);
        byte[] byt=parcel.marshall();

        Bundle newBundle=new Bundle();
        Parcel newParcel=Parcel.obtain();
        newParcel.unmarshall(byt, 0, byt.length);
        bundle.readFromParcel(newParcel);



        Intent intent=new Intent();
    intent.putExtras(newBundle);


        startActivity((Intent) intent.getExtras().get(Intent.EXTRA_SHORTCUT_INTENT));

the newBundle doesnt look exactly the same as the original bundle and its still crashing. So something is still wrong.....

like image 917
treesoft Avatar asked Jun 24 '14 10:06

treesoft


1 Answers

Just to complete this thread/help others........

pskink helped me to find the following solution:

Bundle bundle = shortcutIntent.getExtras();

Parcel parcel = Parcel.obtain();
bundle.writeToParcel(parcel, 0);
byte[] byt = parcel.marshall();

String s = Base64.encodeToString(byt, 0, byt.length, 0); //store this string to sqlite
byte[] newByt = Base64.decode(s, 0);

Bundle newBundle = new Bundle();
Parcel newParcel = Parcel.obtain();
newParcel.unmarshall(newByt, 0, newByt.length);
newParcel.setDataPosition(0);
newBundle.readFromParcel(newParcel);

Intent intent = new Intent();
intent.putExtras(newBundle);

MainActivity.getContext().startActivity((Intent)intent.getExtras().get(Intent.EXTRA_SHORTCUT_INTENT));

Thank you pskink so much again!

like image 94
treesoft Avatar answered Sep 30 '22 10:09

treesoft