2011-01-25 224 views
1

在我的應用我已經是註冊一個BroadcastReceiver爲onStart()方法的服務:註銷廣播接收器

public void onStart() { 
    if(something....) { 
     IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
     filter.addAction(Intent.ACTION_SCREEN_OFF); 
     screenReceiver = new ScreenReceiver(); 
     registerReceiver(screenReceiver, filter); 
    } else { 
     if(screenReceiver != null) { 
      unregisterReceiver(screenReceiver); 
     } 
    } 
} 

和它工作正常。 不過,我想註銷內else這個廣播接收器,我收到此錯誤:

01-25 09:30:03.730: ERROR/AndroidRuntime(11748): FATAL EXCEPTION: main 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748): java.lang.RuntimeException: Unable to start service [email protected] with Intent { cmp=com.myservice/.Service (has extras) }: java.lang.IllegalArgumentException: Receiver not registered: [email protected] 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at android.app.ActivityThread.access$3600(ActivityThread.java:125) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at android.os.Looper.loop(Looper.java:123) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at java.lang.reflect.Method.invoke(Method.java:521) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at dalvik.system.NativeStart.main(Native Method) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748): Caused by: java.lang.IllegalArgumentException: Receiver not registered: [email protected] 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at android.app.ActivityThread$PackageInfo.forgetReceiverDispatcher(ActivityThread.java:793) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:814) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:331) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at com.myservice.Service.onStart(Service.java:291) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at android.app.Service.onStartCommand(Service.java:420) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053) 
01-25 09:30:03.730: ERROR/AndroidRuntime(11748):  ... 10 more 

你能幫助我嗎?

回答

4

修改你的代碼如下

public void onStart() { 
    if(something....) { 
     IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); 
     filter.addAction(Intent.ACTION_SCREEN_OFF); 
     screenReceiver = new ScreenReceiver(); 
     registerReceiver(screenReceiver, filter); 
    } else { 
     if(screenReceiver != null) { 
      unregisterReceiver(screenReceiver); 
      screenReceiver = null; 
     } 
    } 
} 

調用unregisterReceiver()不設置screenReceiver爲null,當控制達到該塊,也未必永遠是你的接收器被註冊的情況。 例如按順序考慮以下內容:

  1. if(something...)爲真,因此screenReceiver被實例化並註冊。
  2. if(something...)這次是錯誤的,所以screenReceiver是未註冊的。
  3. if(something...)又是錯誤的,在這裏,screenReceiver在步驟1中被實例化,因此它不是null,但它在步驟2中未註冊,因此該例外「Receiver not registered:[email protected]」。

screenReceiver設置爲空後,請考慮按順序在3個以上的步驟。它會正確執行。

+0

你有權利。謝謝!! – CeccoCQ 2011-01-25 09:40:15