0

嗨,我在注册广播接收器时遇到问题我不知道为什么这是我的代码

主要活动

public class Service extends Activity {
private MyServiceReceiver myReceiver;
TextView txtManual;
TextView txtAuto;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_service);



    IntentFilter filter;
    filter = new IntentFilter(MyService.NEW_DATA);
    myReceiver = new MyServiceReceiver();
    registerReceiver(myReceiver, filter);


txtAuto = (TextView) findViewById(R.id.txtAuto);
txtManual = (TextView) findViewById(R.id.txtManual);

Button cmdStart = (Button) findViewById(R.id.cmdStart);

cmdStart.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        startService(new Intent(Service.this, MyService.class));
    }
});

Button cmdRefresh = (Button) findViewById(R.id.cmdRefresh);

}

public class MyServiceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
    txtAuto.setText(intent.getStringExtra("data")); 
    if(intent.getStringExtra("data")== null){

        txtManual.setText("Null");

    }
    }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override


protected void onPause() {
    // TODO Auto-generated method stub
    unregisterReceiver(myReceiver);
    super.onPause();
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    unregisterReceiver(myReceiver);

    super.onStop();
}
@Override
protected void onResume() {
    // TODO Auto-generated method stub

    IntentFilter filter;
    filter = new IntentFilter(MyService.NEW_DATA);
    myReceiver = new MyServiceReceiver();
    registerReceiver(myReceiver, filter);

    super.onResume();
}}

这是我的清单:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.t8ej1"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="17"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>

<application

    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <service android:name="MyService"/>



    <activity
        android:name="com.example.t8ej1.Service"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

And this is my logcat




 07-08 01:44:26.328: E/AndroidRuntime(6315): FATAL EXCEPTION: main
 07-08 01:44:26.328: E/AndroidRuntime(6315): java.lang.RuntimeException: Unable to stop       activity    
 {com.example.t8ej1/com.example.t8ej1.Service}: java.lang.IllegalArgumentException: Receiver not    registered: com.example.t8ej1.Service$MyServiceReceiver@40d10a30
 07-08 01:44:26.328: E/AndroidRuntime(6315):    at       android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3415)
 07-08 01:44:26.328: E/AndroidRuntime(6315):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3469)
  07-08 01:44:26.328: E/AndroidRuntime(6315):   at android.app.ActivityThread.access$1200(ActivityThread.java:141)
  07-08 01:44:26.328: E/AndroidRuntime(6315):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
 07-08 01:44:26.328: E/AndroidRuntime(6315):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 01:44:26.328: E/AndroidRuntime(6315):     at android.os.Looper.loop(Looper.java:137)
07-08 01:44:26.328: E/AndroidRuntime(6315):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-08 01:44:26.328: E/AndroidRuntime(6315):     at java.lang.reflect.Method.invokeNative(Native Method)
 07-08 01:44:26.328: E/AndroidRuntime(6315):    at java.lang.reflect.Method.invoke(Method.java:511)
07-08 01:44:26.328: E/AndroidRuntime(6315):     at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-08 01:44:26.328: E/AndroidRuntime(6315):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

07-08 01:44:26.328: E/AndroidRuntime(6315): at dalvik.system.NativeStart.main(Native Method) 07-08 01:44:26.328: E/AndroidRuntime(6315): 由: java.lang .IllegalArgumentException:接收器未注册:com.example.t8ej1.Service$MyServiceReceiver@40d10a30 07-08 01:44:26.328: E/AndroidRuntime(6315): at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:657) 07 -08 01:44:26.328: E/AndroidRuntime(6315): 在 android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1339) 07-08 01:44:26.328: E/AndroidRuntime(6315): 在 android.content .ContextWrapper.unregisterReceiver(ContextWrapper.java:445) 07-08 01:44:26.328: E/AndroidRuntime(6315): at com.example.t8ej1.Service.onStop(Service.java:85) 07-08 01:44 :26.328:E/AndroidRuntime(6315):在 android.app.Instrumentation。callActivityOnStop(Instrumentation.java:1205) 07-08 01:44:26.328: E/AndroidRuntime(6315): 在 android.app.Activity.performStop(Activity.java:5246) 07-08 01:44:26.328: E/ AndroidRuntime(6315):在 android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3410)

OnCreate我在and方法中注册了广播接收器并在andOnResume方法中取消注册,但是当我退出应用程序时它会产生错误!OnPauseOnStop

4

1 回答 1

1

那是因为您已经在 onPause 方法中取消了它的注册。如果查看活动生命周期,可以看到 onPause 在 onStop 之前被调用。如何在 onPause 方法中只注销一次,看看是否有 BroadcastReceiver 泄漏?

另一件事,我注意到你在 onResume 方法中再次注册了同一个接收器,这是你真正想要的吗?看生命周期图,肯定对你很方便。:)

于 2013-07-08T02:00:14.870 回答