13

下面的代码片段,从我的 实现中调用onOptionsItemSelected(),可以很好地将用户从我的应用程序带到邮件客户端,并预先填写了电子邮件地址、主题和正文。我将其用作让用户给我反馈的简单方法。

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()可以帮助这种情况。

04-08 21:26:19.094: E/ActivityThread(27894): Activity 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 泄露了 IntentReceiver com.android.internal.app.ResolverActivity$1@4150aac8 原来是在这里注册。您是否错过了对 unregisterReceiver() 的调用?

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

这感觉像是一个 Android 错误,因为我自己的代码没有调用registerReceiver(),那么为什么 Android 抱怨我需要调用unregisterReceiver()呢?

4

2 回答 2

11

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.

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.

More detail:

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

mPackageMonitor.register(this, false);

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.

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

For more info you can see this in code:

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.

于 2012-04-24T01:13:15.830 回答
4

简单解决问题。

更多信息在这里:https ://developer.android.com/training/basics/intents/sending.html

Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);

PackageManager pkManager = getPackageManager();
List<ResolveInfo> activities = pkManager.queryIntentActivities(mapIntent, 0);

if (activities.size() > 1) {
    // Create and start the chooser
    Intent chooser = Intent.createChooser(mapIntent, "Open with");
    startActivity(chooser);

  } else {
    startActivity( mapIntent );
}
于 2013-01-02T10:28:19.750 回答