ChooserActivity has leaked IntentReceiver

2019-01-20 17:46发布

问题:

E/ActivityThread(  655): Activity com.android.internal.app.ChooserActivity has
leaked IntentReceiver com.android.internal.app.ResolverActivity$1@412f4f38 
that was originally registered here. 

Are you missing a call to unregisterReceiver()? E/ActivityThread(  655): 
android.app.IntentReceiverLeaked: 
Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver 
com.android.internal.app.ResolverActivity$1@412f4f38 that was originally 
registered here. Are you missing a call to unregisterReceiver()?
  1. What does this error mean?
  2. Since the application continues working should I ignore it?
  3. How can I fix it?

It occurs when I choose an image from the phone Gallery following the routine posted here. Precisely when I press the Browse Gallery Button defined in the layout of the Activity.


Full LOGCAT:

E/ActivityThread(  655):    at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:763)
E/ActivityThread(  655):    at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
E/ActivityThread(  655):    at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1043)
E/ActivityThread(  655):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1030)
E/ActivityThread(  655):    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1024)
E/ActivityThread(  655):    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:341)
E/ActivityThread(  655):    at com.android.internal.content.PackageMonitor.register(PackageMonitor.java:65)
E/ActivityThread(  655):    at com.android.internal.app.ResolverActivity.onCreate(ResolverActivity.java:99)
E/ActivityThread(  655):    at com.android.internal.app.ChooserActivity.onCreate(ChooserActivity.java:53)
E/ActivityThread(  655):    at android.app.Activity.performCreate(Activity.java:4465)
E/ActivityThread(  655):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/ActivityThread(  655):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/ActivityThread(  655):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/ActivityThread(  655):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/ActivityThread(  655):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/ActivityThread(  655):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/ActivityThread(  655):    at android.os.Looper.loop(Looper.java:137)
E/ActivityThread(  655):    at android.app.ActivityThread.main(ActivityThread.java:4424)
E/ActivityThread(  655):    at java.lang.reflect.Method.invokeNative(Native Method)
E/ActivityThread(  655):    at java.lang.reflect.Method.invoke(Method.java:511)
E/ActivityThread(  655):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/ActivityThread(  655):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/ActivityThread(  655):    at dalvik.system.NativeStart.main(Native Method)

回答1:

I know this is older, but I've had the same issue and think I stumbled across the answer on accident, though it's strange.

Within the emulator, if you do something to cause an application picker such as a share intent or to choose a gallery app it appears to only have an Exception if there is only one application and automatically loads the application without you choosing one.

For example, you want to pick an image. If you run your routine to select an image you will most likely have a few choices appear in a chooser dialog on your phone. However on the emulator, you rarely have anything other than just the default Gallery application.

When it's only one application and it loads, you get an Exception.

If you have multiple applications to choose from, you'll get no Exception.

The same thing happens if you want to share a text file. On the emulator the text file will automatically load up a Messaging application directly and you'll get an Exception.

If you setup the Email application, it'll show two applications in a chooser. You'll have no Exception.

I believe you can safely ignore this since it appears to be an internal error message.

You can try to reproduce this by installing a second gallery application to see it not creating an error message.

I just noticed this also describes the same suggestion https://stackoverflow.com/a/10290486/935779

It has a more complicated work around if you really want to avoid this Exception.



回答2:

This is about life-cycle.

It means that you have registered an IntentReceiver somewhere but it failed to unregister it before your application is paused by the Android.

You should unregister receiver in onPause method and in onRestart re-register receiver. So override these methods and do it as i meant.



回答3:

This error seems to be caused by a bug in Android's source code, since I never registered an Intent Receiver in mine.

You can also check this SO question for details Why does Intent.createChooser() need a BroadcastReceiver and how to implement?.

It clearly says that the ResolverActivity registers a BroadcastReceiver, but doesn't always unregister it.

Furthermore:

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.

But I don't have an idea if the bug was fixed, maybe someone who does could provide more information on it?



回答4:

If you are reading this post and getting this error when only one application is returned as available for the chooser, then it's most likely an Android bug. There is an issue raised here.

From reading other reports, it appears there is nothing that can be done...



回答5:

I'm not 100% sure but if it works fine AND throws the exception probably you need do nothing about it. It can be problem of your device, for example, error in customised by vendor Android OS. Try to test it on emulator, if it wouldn't throw an exception, just leave it as is. I think your code is fine.