繁体   English   中英

将Intent转换为String,反之亦然

[英]Convert Intent to String and vice versa

我打算开始这样的快捷方式活动:

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

我需要将shortcut_intent转换为字符串以将其保存在sqlite db中。 我这么久都试了很久没有成功。 目前我站在这里:

将意图转换为字符串:

String uri_string = mIntent_shortcut_intent.toUri(0);

创造新意图:

Intent intent = new Intent();

并解析来自uri的额外内容:

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

不工作/ app悲伤地崩溃;(

有人可以帮我弄这个吗? 或者告诉我一个替代方法来保存sqlite db中持久的意图?

thx提前


更新:

正如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));

newBundle看起来与原始捆绑包完全不同,但仍然崩溃。 所以有些事情仍然是错的......

只是为了完成这个帖子/帮助别人........

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

谢谢你再次pskink!

你有没有尝试过

String sIntent = intent.toString();

将意图转换为字符串?

暂无
暂无

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

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