注册广播接收器时出错

时间:2012-03-31 06:37:00

标签: android exception broadcastreceiver memory-leaks

我正在编写一个小程序,当收到短信时会被激活。我们需要通过图形用户界面启动/停止服务。我使用复选框和按钮来启动或停止服务。

代码如下。

 private smsBroadcast b;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

 }

public void onclick(View i)
{
    CheckBox c=(CheckBox)findViewById(R.id.checkBox1);
      b=new smsBroadcast();
    try{
    if(c.isChecked())
    registerReceiver(b, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
    else
    unregisterReceiver(b);
    }
    catch(Exception e)
    {

    }
    finish();
}

这里smsBroadcast是broadcastReceiver类 当我们单击按钮时,它会在logcat中显示一些异常。 例外情况如下......

           03-31 12:01:33.923: E/ActivityThread(806): Activity examples.sms.SmssmsActivity has leaked IntentReceiver examples.sms.smsBroadcast@405457f0 that was originally registered here. Are you missing a call to unregisterReceiver()?
           03-31 12:01:33.923: E/ActivityThread(806): android.app.IntentReceiverLeaked: Activity examples.sms.SmssmsActivity has leaked IntentReceiver examples.sms.smsBroadcast@405457f0 that was originally registered here. Are you missing a call to unregisterReceiver()?
           03-31 12:01:33.923: E/ActivityThread(806):   at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:756)
           03-31 12:01:33.923: E/ActivityThread(806):   at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:551)
           03-31 12:01:33.923: E/ActivityThread(806):   at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:795)
           03-31 12:01:33.923: E/ActivityThread(806):   at android.app.ContextImpl.registerReceiver(ContextImpl.java:782)
           03-31 12:01:33.923: E/ActivityThread(806):   at android.app.ContextImpl.registerReceiver(ContextImpl.java:776)
           03-31 12:01:33.923: E/ActivityThread(806):   at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
           03-31 12:01:33.923: E/ActivityThread(806):   at examples.sms.SmssmsActivity.onclick(SmssmsActivity.java:25)
           03-31 12:01:33.923: E/ActivityThread(806):   at java.lang.reflect.Method.invokeNative(Native Method)
           03-31 12:01:33.923: E/ActivityThread(806):   at java.lang.reflect.Method.invoke(Method.java:507)
           03-31 12:01:33.923: E/ActivityThread(806):   at android.view.View$1.onClick(View.java:2139)
           03-31 12:01:33.923: E/ActivityThread(806):   at android.view.View.performClick(View.java:2485)
           03-31 12:01:33.923: E/ActivityThread(806):   at android.view.View$PerformClick.run(View.java:9080)
           03-31 12:01:33.923: E/ActivityThread(806):   at android.os.Handler.handleCallback(Handler.java:587)
           03-31 12:01:33.923: E/ActivityThread(806):   at android.os.Handler.dispatchMessage(Handler.java:92)
           03-31 12:01:33.923: E/ActivityThread(806):   at android.os.Looper.loop(Looper.java:123)
           03-31 12:01:33.923: E/ActivityThread(806):   at android.app.ActivityThread.main(ActivityThread.java:3683)
           03-31 12:01:33.923: E/ActivityThread(806):   at java.lang.reflect.Method.invokeNative(Native Method)
           03-31 12:01:33.923: E/ActivityThread(806):   at java.lang.reflect.Method.invoke(Method.java:507)
           03-31 12:01:33.923: E/ActivityThread(806):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
           03-31 12:01:33.923: E/ActivityThread(806):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
           03-31 12:01:33.923: E/ActivityThread(806):   at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

您需要在onResume中注册接收器并在onPause中取消注册

http://developer.android.com/reference/android/content/BroadcastReceiver.html

如果在Activity.onResume()实现中注册接收者,则应在Activity.onPause()中取消注册。 (暂停时你不会收到意图,这将减少不必要的系统开销)。不要在Activity.onSaveInstanceState()中取消注册,因为如果用户在历史堆栈中向后移动,则不会调用它。

答案 1 :(得分:0)

似乎您创建了比Android可以处理的更多IntentReceivers。当您不需要更多Receiver时,您应该使用unregisterReceiver()

相关问题