从未知原因中清除了额外的意图 [英] Intent extra gets cleared out from unknown reason

查看:64
本文介绍了从未知原因中清除了额外的意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有活动 A ,该活动以以下代码开始活动 B :

I have Activity A which starts Activity B with following code:

Intent intent = new Intent(this, B.class);
intent.putExtra("foo", new MySerializableObject());
startActivity(intent);

B 中正确接收到"foo",然后创建PendingIntent以在一段时间后自行启动,您可以将其视为一些闹钟应用程序.无论如何,神秘的事情是当我按以下方式安排此意图时:

In B "foo" is received correctly and then I create PendingIntent to start itself after some time, you can think about it as some alarm clock app. Anyway the mysterious thing is that when I schedule this intent in following way:

Intent intent = new Intent(context, B.class);
intent.putExtra("bar", true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
        SystemClock.elapsedRealtime() + delayMs, pendingIntent);

然后一切都很好(在收到此意图后,"bar"值为true),但是如果我在"bar"之前或之后添加以下行:

Then everything is fine (after receiving this intent "bar" value is true), however if I add following line before or after "bar":

intent.putExtra("foo", new MySerializableObject());

然后,当我收到此意图时,"foo"和"bar"都丢失了.我的意思是这两行都返回false:

Then when I receive this intent both "foo" and "bar" are missing. I mean false is returned from both of those lines:

getIntent().hasExtra("foo")
getIntent().hasExtra("bar")

这种行为的原因可能是什么?

What could be the reason of such behaviour?

根据我尝试过的评论中的建议:

Basing on suggestion in comments I've tried:

intent.putExtra("foo", true);
intent.putExtra("bar", true);

并且有效,所以我认为MySerializableObject可能有问题,所以这是我接下来尝试的方法:

and it worked, so I thought that maybe there is something wrong with MySerializableObject, so this is what I've tried next:

intent.putExtra("foo",
        new Serializable() {
            @Override
            public int hashCode() { return super.hashCode(); }
            });
intent.putExtra("bar", true);

但这会导致与我描述的问题完全相同的问题(缺少"foo"和"bar").最终,我也尝试将"foo"替换为"xxx",但它并没有改变任何内容,因此对我来说,它看起来像是一些奇怪的Android错误.

But this causes exactly the same problem as I described ("foo" and "bar") are missing. Finally I've also tried replacing "foo" with "xxx" but it didn't change anything, so to me it looks like some weird Android bug.

推荐答案

android-hub 所建议已经尝试使用Bundle,但问题是相同的.可以使用(栏"可用):

As suggested by android-hub I've tried using Bundle but the problem is the same. This works ("bar" is available):

    Bundle b = new Bundle();
    b.putBoolean("bar", true);
    intent.putExtras(b);

这不存在("xxx"和"bar"都不存在):

while this doesn't (both "xxx" and "bar" don't exist):

    Bundle b = new Bundle();
    b.putSerializable("xxx", new Serializable() {});
    b.putBoolean("bar", true);
    intent.putExtras(b);

提到的答案解释了一些情况,但是如您所见,建议的解决方案(与Bundle一起使用)不起作用.无论如何,我猜这部分是正确的解释:

The mentioned answer explains a bit what is going on, however as you see suggested solution (with Bundle) doesn't work. Anyway I guess this part is a correct explanation:

如果核心OS进程需要修改Intent Extras,则该过程将尝试重新创建您的Serializable对象,这是设置Extras Bundle进行修改的一部分.该进程没有您的类,因此它会遇到运行时异常.

if a core OS process needs to modify the Intent extras, that process winds up trying to recreate your Serializable objects as part of setting up the extras Bundle for modification. That process does not have your class and so it gets a runtime exception.

在阅读后,我在日志中看到了这一点:

And after reading that I saw this in logs:

W/Intent:未能额外填写java.lang.RuntimeException: Parcelable在读取Serializable时遇到ClassNotFoundException 对象(名称= test.edu.B $ 1)

W/Intent: Failure filling in extras java.lang.RuntimeException: Parcelable encountered ClassNotFoundException reading a Serializable object (name = test.edu.B$1)

以前,我错过了这一点,因为我只是在查看应用程序中的日志.无论如何,我可能都必须将MySerializableObject分解为本机类型(字符串,int数组等)为它们等创建特殊的键,我想这会起作用但很痛苦.我不想使用Parcelable(即使它可以工作),因为我不想让模型类特定于Android.

Previously I missed that because I was only looking at logs from my app. Anyway I will probably have to decompose MySerializableObject to native types (Strings, int arrays etc) create special keys for them and etc, which I guess will work but is painful. I don't want to use Parcelable (even if it would work) because I don't want my model classes to be Android specific.

在浪费您和我几个小时的时间来确定此问题之后,我唯一能说的就是Google在这种特殊情况下做出了非常糟糕的设计.我认为PendingIntent应该检查是否向我的意图中添加了一些可序列化的东西,并在我的应用程序中抛出了一些不受支持的异常,或者如果没有其他Android组件不能很好地处理,则应该没有API可以将Serializable最初添加到Intent中.

The only thing I can say after wasting couple hours of your and my time to identify this problem is that Google made really crappy design in that particular case. In my opinion PendingIntent should check if some serializable is added to intent and throw some unsupported exception already in my app or there shouldn't be API for adding Serializable to Intent in the first place if it can't be handled well with other Android components.

这篇关于从未知原因中清除了额外的意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆