开发者

Broadcast receiver crashes after changing screen orientation and do not work

开发者 https://www.devze.com 2023-04-13 01:17 出处:网络
I had 2 BroadcastReceivers for watching SMS status. Code: //---when the SMS has been sent--- registerReceiver(new BroadcastReceiver(){

I had 2 BroadcastReceivers for watching SMS status. Code:

//---when the SMS has been sent---
   registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
        ...................
        ...................
        }, new IntentFilter(SENT));
    //---when the SMS has bee开发者_StackOverflow中文版n delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
        ...................
        ...................
        }, new IntentFilter(DELIVERED));

Those two receivers are located in a procedure called initSMS (). initSMS () is called once in OnCreate () method. Everything works fine, but the following situation disappoints me. When a user changes the screen orientation android throws an exception, application is suppressed. LogCat shows me that the program does not want to register BroadcastReceiver the second time. I thought that there are no problems and added a Boolean flag to skip the call initSMS () next time in OnCreate (). But even if the second time the call of initSMS () is skipped, the first time changing of orientation of the screen get here is a new bug in LogCat::

Activity has leaked IntentReceiver

Once this happens, it is impossible to track the status of SMS, the receivers do not work. But if the user tries to play and continue to change the orientation, the error does not appear in any case.

--

Who knows, what happened? The workaround like a block permission to change orientation not interesting. I need to understand what happened here and solve a current problem.

LOGcat

> 10-15 20:08:20.863: ERROR/ActivityThread(452): Activity
> com.example.android.Dima.MySoft has leaked IntentReceiver
> com.example.android.Dima.MySoft$3@40531880 that was originally
> registered here. Are you missing a call to unregisterReceiver()? 10-15
> 20:08:20.863: ERROR/ActivityThread(452):
> android.app.IntentReceiverLeaked: Activity
> com.example.android.Dima.MySoft has leaked IntentReceiver
> com.example.android.Dima.MySoft$3@40531880 that was originally
> registered here. Are you missing a call to unregisterReceiver()? 10-15
> 20:08:20.863: ERROR/ActivityThread(452): at
> android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:799)
> 10-15 20:08:20.863: ERROR/ActivityThread(452): at
> android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:575) 10-15
> 20:08:20.863: ERROR/ActivityThread(452): at
> android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:852)
> 10-15 20:08:20.863: ERROR/ActivityThread(452): at
> android.app.ContextImpl.registerReceiver(ContextImpl.java:839) 10-15
> 20:08:20.863: ERROR/ActivityThread(452): at
> android.app.ContextImpl.registerReceiver(ContextImpl.java:833) 10-15
> 20:08:20.863: ERROR/ActivityThread(452): at
> android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
> 10-15 20:08:20.863: ERROR/ActivityThread(452): at
> com.example.android.Dima.MySoft.initSMS(MySoft.java:473) 10-15
> 20:08:20.863: ERROR/ActivityThread(452): at
> com.example.android.Dima.MySoft.onCreate(MySoft.java:263) 10-15
> 20:08:20.863: ERROR/ActivityThread(452): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
> 10-15 20:08:20.863: ERROR/ActivityThread(452): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
> 10-15 20:08:20.863: ERROR/ActivityThread(452): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
> 10-15 20:08:20.863: ERROR/ActivityThread(452): at
> android.app.ActivityThread.access$1500(ActivityThread.java:132) 10-15
> 20:08:20.863: ERROR/ActivityThread(452): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
> 10-15 20:08:20.863: ERROR/ActivityThread(452): at
> android.os.Handler.dispatchMessage(Handler.java:99) 10-15
> 20:08:20.863: ERROR/ActivityThread(452): at
> android.os.Looper.loop(Looper.java:143) 10-15 20:08:20.863:
> ERROR/ActivityThread(452): at
> android.app.ActivityThread.main(ActivityThread.java:4268) 10-15
> 20:08:20.863: ERROR/ActivityThread(452): at
> java.lang.reflect.Method.invokeNative(Native Method) 10-15
> 20:08:20.863: ERROR/ActivityThread(452): at
> java.lang.reflect.Method.invoke(Method.java:507) 10-15 20:08:20.863:
> ERROR/ActivityThread(452): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
> 10-15 20:08:20.863: ERROR/ActivityThread(452): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 10-15
> 20:08:20.863: ERROR/ActivityThread(452): at
> dalvik.system.NativeStart.main(Native Method) 10-15 20:08:20.873:
> ERROR/ActivityThread(452): Activity com.example.android.Dima.MySoft
> has leaked IntentReceiver com.example.android.Dima.MySoft$4@40531f08
> that was originally registered here. Are you missing a call to
> unregisterReceiver()? 10-15 20:08:20.873: ERROR/ActivityThread(452):
> android.app.IntentReceiverLeaked: Activity
> com.example.android.Dima.MySoft has leaked IntentReceiver
> com.example.android.Dima.MySoft$4@40531f08 that was originally
> registered here. Are you missing a call to unregisterReceiver()? 10-15
> 20:08:20.873: ERROR/ActivityThread(452): at
> android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:799)
> 10-15 20:08:20.873: ERROR/ActivityThread(452): at
> android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:575) 10-15
> 20:08:20.873: ERROR/ActivityThread(452): at
> android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:852)
> 10-15 20:08:20.873: ERROR/ActivityThread(452): at
> android.app.ContextImpl.registerReceiver(ContextImpl.java:839) 10-15
> 20:08:20.873: ERROR/ActivityThread(452): at
> android.app.ContextImpl.registerReceiver(ContextImpl.java:833) 10-15
> 20:08:20.873: ERROR/ActivityThread(452): at
> android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
> 10-15 20:08:20.873: ERROR/ActivityThread(452): at
> com.example.android.Dima.MySoft.initSMS(MySoft.java:511) 10-15
> 20:08:20.873: ERROR/ActivityThread(452): at
> com.example.android.Dima.MySoft.onCreate(MySoft.java:263) 10-15
> 20:08:20.873: ERROR/ActivityThread(452): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
> 10-15 20:08:20.873: ERROR/ActivityThread(452): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
> ....

Also confuses a next phrase from LOGCat

Are you missing a call to unregisterReceiver()?

... why this happens?


When a user changes the screen orientation android throws an exception application is suppressed.

You should unregister the receivers in, say, onPause() and register them in onResume(). That way, when Android destroys and recreates the activity for the configuration change, you will still have receivers set up.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号