我有2個BroadcastReceivers用於收看SMS狀態。 代碼:廣播接收機在改變屏幕方向後崩潰並且不工作
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
...................
...................
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
...................
...................
}, new IntentFilter(DELIVERED));
這兩個接收器都位於一個稱爲initSMS程序()。在OnCreate()方法中調用initSMS()一次。一切正常,但下面的情況令我失望。當用戶改變屏幕方向時,android會拋出異常,應用程序被禁止。 LogCat顯示我的程序不想第二次註冊BroadcastReceiver。我認爲沒有任何問題,並且下次在OnCreate()中添加一個布爾標誌來跳過調用initSMS()。但即使initSMS()的調用被跳過第二次,第一次屏幕到這裏的朝向改變是logcat的一個新的bug ::
活動已泄漏IntentReceiver
一旦發生這種情況,就不可能跟蹤SMS的狀態,接收器不起作用。但是,如果用戶嘗試播放並繼續更改方向,則在任何情況下都不會出現錯誤。
-
誰知道,發生了什麼事? 解決方案就像塊權限更改方向不感興趣。我需要了解發生在這裏的事情並解決當前的問題。
logcat的
> 10-15 20:08:20.863: ERROR/ActivityThread(452): Activity
> com.example.android.Dima.MySoft has leaked IntentReceiver
> [email protected] 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
> [email protected] 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 [email protected]
> 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
> [email protected] 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)
> ....
而且混淆了logcat的
下一個短語你缺少unregisterReceiver()的調用?
...爲什麼會發生這種情況?
非常感謝您的幫助!一切正常,清晰! – Dimon