为什么Intent.createChooser()需要一个BroadcastReceiver以及如何实现? [英] Why does Intent.createChooser() need a BroadcastReceiver and how to implement?

查看:170
本文介绍了为什么Intent.createChooser()需要一个BroadcastReceiver以及如何实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码片段,从我实施 onOptionsItemSelected()的调用,很好地工作,从我的应用程序进行用户邮件客户端的电子邮件地址,主题和正文pre-填补。我用这个作为一个简单的方法,让用户给我反馈。

The following snippet, called from my implementation of onOptionsItemSelected(), works nicely to carry the user from my app to a mail client with email address, subject and body pre-filled. I'm using this as a simple way to let the user give me feedback.

String uriText =
    "mailto:" + emailAddress +
    "?subject=" + subject +
    "&body=" + body;

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse(uriText));
startActivity(Intent.createChooser(emailIntent, "Pick an email app:"));

在邮件应用程序中打开(在我的Nexus S与Android 4.0.4),LogCat中输出以下内容,我想不通为什么;谷歌和SO为的 createChooser unregisterReceiver 的似乎无果而终,而我无法找到的例子很多createChooser()也叫 unregisterReceiver()的方式,帮助这种情况。

When the mail app opens (on my Nexus S with Android 4.0.4), LogCat outputs the following, and I can't figure out why; Google and SO searches for createChooser unregisterReceiver seem fruitless, and I can't find many examples of createChooser() that also call unregisterReceiver() in a way that helps this situation.

04-08 21:26:19.094:E / ActivityThread(27894):活动com.android.internal.app.ChooserActivity渗漏最初注册到了这里IntentReceiver com.android.internal.app.ResolverActivity$1@4150aac8。是否缺少调用unregisterReceiver()?

04-08 21:26:19.094: E/ActivityThread(27894): Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@4150aac8 that was originally registered here. Are you missing a call to unregisterReceiver()?

04-08 21:26:19.094:E / ActivityThread(27894):android.app.IntentReceiverLeaked:活动com.android.internal.app.ChooserActivity渗漏IntentReceiver com.android.internal.app.ResolverActivity$1@4150aac8最初这里注册。是否缺少调用unregisterReceiver()?

04-08 21:26:19.094: E/ActivityThread(27894): android.app.IntentReceiverLeaked: Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@4150aac8 that was originally registered here. Are you missing a call to unregisterReceiver()?

04-08 21:26:19.094:E / ActivityThread(27894):在android.app.LoadedApk $ ReceiverDispatcher(LoadedApk.java:763)

04-08 21:26:19.094: E/ActivityThread(27894): at android.app.LoadedApk$ReceiverDispatcher.(LoadedApk.java:763)

这感觉就像一个Android的错误,因为我自己的code不叫 registerReceiver(),为什么是Android的抱怨,我需要调用 unregisterReceiver()

This feels like an Android bug because my own code doesn't call registerReceiver(), so why is Android complaining that I need to call unregisterReceiver()?

推荐答案

我看到这个问题,以及对我的Galaxy Nexus的使用4.0.4,但前提只有一个选项和选择器不会出现。

I see this as well on my Galaxy Nexus with 4.0.4, but only if there's only one option and the chooser doesn't appear.

这是Android的源代码中的错误 - 没有什么可以做这件事。其ResolverActivity寄存器一个BroadcastReceiver,但并不总是注销它

This is a bug in Android source - not much you can do about it. Their ResolverActivity registers a BroadcastReceiver, but doesn't always unregister it.

更多细节:

Intent.createChooser()将启动一个ResolverActivity。在的onCreate(),该活动要求

Intent.createChooser() will start a ResolverActivity. In onCreate(), the activity calls

mPackageMonitor.register(this, false);

mPackageMonitor是一个BroadcastReceiver,并在寄存器()它本身注册上的活动。通常情况下,接收器在注销的onStop()。但是,后来在的onCreate()的code盘有多少选项,用户可以选择。如果只有一个,它调用完成()。由于完成()被称为的onCreate()的其他生命周期方法不会被调用,并跳转直接的onDestroy() - 漏接收器

mPackageMonitor is a BroadcastReceiver and within register() it registers itself on the activity. Normally, the receiver is unregistered in onStop(). However, later in onCreate() the code checks how many options the user can choose from. If there's only one it calls finish(). Since finish() is called in onCreate() the other lifecycle methods are never called and it jumps straight to onDestroy() - leaking the receiver.

我没有看到一个bug这个在Android问题的数据库,所以我的创建了一个

I didn't see a bug for this in the Android issues database, so I created one.

有关更多信息,你可以看到这一点code:

For more info you can see this in code:

  • <一个href="https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/app/ResolverActivity.java">ResolverActivity
  • <一个href="https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/content/PackageMonitor.java">PackageMonitor
  • ResolverActivity
  • PackageMonitor

作为一个侧面说明,谷歌使用电子邮件作为,当你不希望使用一个选择器,所以你可以考虑刚启动的目的通常的例子。查看javadoc,了解<一href="https://github.com/android/platform_frameworks_base/blob/master/core/java/android/content/Intent.java">Intent#ACTION_CHOOSER.

As a side note, Google uses email as an example of when you wouldn't want to use a chooser so you may consider just launching the intent normally. See the javadocs for Intent#ACTION_CHOOSER.

这篇关于为什么Intent.createChooser()需要一个BroadcastReceiver以及如何实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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