java - 将 Intent 转换为字符串,反之亦然

标签 java android sqlite android-intent

我打算开始这样的快捷 Activity :

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

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

将 Intent 转换为字符串:

String uri_string = mIntent_shortcut_intent.toUri(0);

创建新 Intent :

Intent intent = new Intent();

并从 uri 中解析额外内容:

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

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

有人可以帮我吗?或者告诉我另一种方法来保存在 sqlite 数据库中持久的 Intent ?

提前谢谢


更新:

正如 pskink 建议打包的那样,解码 extras-bundle,反之亦然,我做了以下操作:

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!

关于java - 将 Intent 转换为字符串,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24384681/

相关文章:

javascript - window.location.reload() 在我的 js 页面中使用 我正在注销 我正在使用 spring security

java - @SQLResultSetMapping 未找到列问题

JavaFX GraphicsContext 倒圆

java - 在displayMethod中使用printf和forEach,如何更改单个数组显示的间距

android - 水平 ScrollView 和 ImageView ?

java - 如何在 Android 中清除 Canvas 上的绘图

android - 如何每秒更新 RecyclerView 中的文本?

android - 原始查询不工作android

sqlite - INSERT或IGNORE INTO不起作用

python - 操作错误 : no such table: entries while "init_db()" already inserted in flaskr. py