简体   繁体   English

将Intent转换为String,反之亦然

[英]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. 我需要将shortcut_intent转换为字符串以将其保存在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: 并解析来自uri的额外内容:

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

not working/app crashing sadly ;( 不工作/ app悲伤地崩溃;(

can someone help me with this? 有人可以帮我弄这个吗? or tell me an alternative to save an intent persistent in sqlite db? 或者告诉我一个替代方法来保存sqlite db中持久的意图?

thx in advance thx提前


UPDATE: 更新:

as pskink suggested to parcel, unmarshall the extras-bundle and vice versa i did the following: 正如pskink建议包裹,解组附加组件,反之亦然,我做了以下事情:

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. newBundle看起来与原始捆绑包完全不同,但仍然崩溃。 So something is still wrong..... 所以有些事情仍然是错的......

Just to complete this thread/help others........ 只是为了完成这个帖子/帮助别人........

pskink helped me to find the following solution: pskink帮我找到了以下解决方案:

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! 谢谢你再次pskink!

Have you tried just doing 你有没有尝试过

String sIntent = intent.toString();

to convert intent into string? 将意图转换为字符串?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM