将意图转换为字符串,反之亦然

tre*_*oft 5 java sqlite android android-intent

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

startActivity((Intent) shortcut_intent.getExtras().get(Intent.EXTRA_SHORTCUT_INTENT));
Run Code Online (Sandbox Code Playgroud)

我需要将shortcut_intent 转换为字符串以将其保存在sqlite db 中。我尝试了很长时间,但没有成功。目前我站在这里:

将意图转换为字符串:

String uri_string = mIntent_shortcut_intent.toUri(0);
Run Code Online (Sandbox Code Playgroud)

创建新意图:

Intent intent = new Intent();
Run Code Online (Sandbox Code Playgroud)

并从 uri 解析额外内容:

intent.putExtra("android.intent.extra.shortcut.INTENT", Uri.parse(uri_string));
Run Code Online (Sandbox Code Playgroud)

不工作/应用程序崩溃;(

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

提前谢谢


更新:

正如 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));
Run Code Online (Sandbox Code Playgroud)

newBundle 看起来与原始捆绑包并不完全相同,并且仍在崩溃。所以还是有些不对劲......

tre*_*oft 6

只是为了完成这个线程/帮助其他人......

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));
Run Code Online (Sandbox Code Playgroud)

再次非常感谢 pskink!