在Android的API 19(4.4)的intent.createChooser方法导致IntentServiceLeak方法、API、Android、IntentServiceLeak

2023-09-08 08:47:36 作者:清茶白事欢

我的运行全新的Andr​​oid奇巧设备上的应用程序(API 19 4.4),我得到复制到剪贴板每次我试图创建一个Intent选择器。这是发生在Youtube,和的tumblr在Android奇巧各种其他应用程序。纵观日志我看到以下异常:

Running my app on the new Android KitKat device (API 19, 4.4) I get "Copied to Clipboard" everytime I try to create an Intent chooser. This is happening on Youtube, Tumblr and various other apps on Android KitKat. Looking at the logs I'm seeing the following exception:

com.android.internal.app.ChooserActivity渗漏IntentReceiver com.android.internal.app.ResolverActivity$1@4150aac8

com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@4150aac8

这曾经是一个问题引起的,当一个设备没有多个应用程序,以意图(见Why确实Intent.createChooser()需要一个BroadcastReceiver,以及如何实现?)。然而,这不是我的设备上的情况。似乎就象是在Android的API 19被打破。

This used to be an issue caused when a device didn't have multiple apps to Intent to (see Why does Intent.createChooser() need a BroadcastReceiver and how to implement?). However, this is not the case on my device. Seems like something is broken in Android API 19.

推荐答案

下面是我对这个问题的解决方法解决方案。我第一次检测,如果该设备上KIT_KAT或更高的运行,并且,而不是创建一个选择器,我只是尝试启动的意图。这将导致Android的要求,他们要完成的动作(除非用户已经有一个默认为所有ACTION_SEND意图这些应用程序的用户。

Here's my workaround solution for this issue. I first detect if the device is running on KIT_KAT or higher, and instead of creating a chooser, I simply try to start the intent. This will cause Android to ask the user which application they want to complete the action with (unless the user already has a default for all ACTION_SEND intents.

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(Intent.EXTRA_TEXT, message);
sendIntent.setType("text/plain");

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    // This will open the "Complete action with" dialog if the user doesn't have a default app set.
    context.startActivity(sendIntent);
} else {
    context.startActivity(Intent.createChooser(sendIntent, "Share Via"));
}