界面如下:
问题1:点击“解绑广播接收器“后再次点击”解绑广播接收器“后,程序崩溃,log信息如下:
08-04 05:04:35.420: E/AndroidRuntime(5521): FATAL EXCEPTION: main
08-04 05:04:35.420: E/AndroidRuntime(5521): java.lang.IllegalArgumentException: Receiver not registered: com.jinhoward.broadcast.receiver.SecondReceiver@40cfec98
08-04 05:04:35.420: E/AndroidRuntime(5521): at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:657)
08-04 05:04:35.420: E/AndroidRuntime(5521): at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1339)
08-04 05:04:35.420: E/AndroidRuntime(5521): at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:445)
08-04 05:04:35.420: E/AndroidRuntime(5521): at com.jinhoward.broadcast.activity.MainActivity$3.onClick(MainActivity.java:62)
08-04 05:04:35.420: E/AndroidRuntime(5521): at android.view.View.performClick(View.java:4204)
08-04 05:04:35.420: E/AndroidRuntime(5521): at android.view.View$PerformClick.run(View.java:17355)
08-04 05:04:35.420: E/AndroidRuntime(5521): at android.os.Handler.handleCallback(Handler.java:725)
08-04 05:04:35.420: E/AndroidRuntime(5521): at android.os.Handler.dispatchMessage(Handler.java:92)
08-04 05:04:35.420: E/AndroidRuntime(5521): at android.os.Looper.loop(Looper.java:137)
08-04 05:04:35.420: E/AndroidRuntime(5521): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-04 05:04:35.420: E/AndroidRuntime(5521): at java.lang.reflect.Method.invokeNative(Native Method)
08-04 05:04:35.420: E/AndroidRuntime(5521): at java.lang.reflect.Method.invoke(Method.java:511)
08-04 05:04:35.420: E/AndroidRuntime(5521): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-04 05:04:35.420: E/AndroidRuntime(5521): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-04 05:04:35.420: E/AndroidRuntime(5521): at dalvik.system.NativeStart.main(Native Method)
后来排查代码百度找到问题答案,通过一个布尔变量来判断广播接收器是否已经注册。
package com.jinhoward.broadcast.activity; import android.app.Activity; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import com.jinhoward.broadcast.receiver.SecondReceiver; public class MainActivity extends Activity { protected static final String ACTION = "com.jinhoward.broadcast.ACTION"; private Button btnBroadcast; private Button registerReceiver; private Button unregisterReceiver; private SecondReceiver receiver; private Boolean registerBoolean; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnBroadcast = (Button) findViewById(R.id.btnBroadcast); btnBroadcast.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(ACTION); sendBroadcast(intent); } }); registerReceiver = (Button) findViewById(R.id.btnregisterReceiver); registerReceiver.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { receiver = new SecondReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(ACTION); Log.i("MyReceiver", "注册广播接收器!"); registerReceiver(receiver, filter); registerBoolean = true; } }); unregisterReceiver = (Button) findViewById(R.id.btnunregisterReceiver); unregisterReceiver.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //第一次进入到应用程序后就点击“解绑广播接收器”按钮会崩溃,此行代码处理此异常。 if (receiver == null) { Toast.makeText(MainActivity.this, "广播接收器为空!",Toast.LENGTH_SHORT).show(); } //点击“解绑广播接收器“后再次点击”解绑广播接收器“后,程序崩溃,错误log信息为:FATAL EXCEPTION: main //08-04 05:04:35.420: E/AndroidRuntime(5521): java.lang.IllegalArgumentException: Receiver not registered: else if (registerBoolean == false) { Toast.makeText(MainActivity.this, "广播接收器未注册!",Toast.LENGTH_SHORT).show(); }else{ unregisterReceiver(receiver); registerBoolean = false; Log.i("MyReceiver", "广播接收器已经取消注册!"); } } }); } }
package com.jinhoward.broadcast.receiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * @author jinhoward * @blog http://blog.csdn.net/jinhoward */ public class SecondReceiver extends BroadcastReceiver { private static final String TAG = "MyReceiver"; public SecondReceiver() { Log.i(TAG, "广播接收器的构造函数"); } @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "广播接收器接收到广播:"+intent.getAction()); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jinhoward.broadcast.activity" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name="com.jinhoward.broadcast.activity.MainActivity" 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> </manifest>