【问题标题】:Convert Intent to String and vice versa将意图转换为字符串,反之亦然
【发布时间】:2014-06-24 10:48:16
【问题描述】:

我打算像这样开始一个快捷方式活动:

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

无法正常工作/应用程序崩溃了;(

有人可以帮我解决这个问题吗?或者告诉我在 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));

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

【问题讨论】:

  • 你能发布日志堆栈吗?
  • 使用String action=intent.getAction();并将其存储到sqlite。
  • @Ketan Ahir:在我的快捷方式中,Action 始终为“null”。只有“Extras”才有价值,所以我认为它没用......
  • 使用此构造函数创建意图 Intent i=new Intent("your action");
  • 意图意图 = Intent.parse(uri_string)

标签: java android sqlite android-intent


【解决方案1】:

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

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!

【讨论】:

    猜你喜欢
    • 2011-05-27
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多