要了解broadCastReceivers
是如何工作的,我创建了一些小例子并遵循了这个例子。
当我创建下面显示的mBroadCastReceiver_0
时,应用得很好,但是当我添加第二个broadCastReceiver
mBroadCastReceiver_1
时,应用程序崩溃了,logcat
生成了以下错误。请问任何人,请告诉我代码的内容是什么?
代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SetUpAllViews(R.layout.activity_main);
this.registerReceiver(this.mBroadCastReceiver_0, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
ConnectWiFi();
this.registerReceiver(this.mBroadCastReceiver_1, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
....
....
....
private BroadcastReceiver mBroadCastReceiver_0 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int extraWiFiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN);
switch (extraWiFiState) {
case WifiManager.WIFI_STATE_ENABLING: setText(R.id.tv_conn_status, "Enabling..."); break;
case WifiManager.WIFI_STATE_ENABLED: setText(R.id.tv_conn_status, "Enabled"); break;
case WifiManager.WIFI_STATE_DISABLING: setText(R.id.tv_conn_status, "Disabling..."); break;
case WifiManager.WIFI_STATE_DISABLED: setText(R.id.tv_conn_status, "Disabled"); break;
case WifiManager.WIFI_STATE_UNKNOWN: setText(R.id.tv_conn_status, "Unkown_WiFi_State"); break;
}
}
};
private BroadcastReceiver mBroadCastReceiver_1 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
WifiInfo wifiInfo = intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
int linkSpeed = wifiInfo.getLinkSpeed();
setText(R.id.tv_conn_lspeed, "LinkSpeed: "+linkSpeed);
}
};
logcat的:
11-07 19:31:53.429: E/AndroidRuntime(6957): FATAL EXCEPTION: main
11-07 19:31:53.429: E/AndroidRuntime(6957): Process: com.example.wifi_socket_00, PID: 6957
11-07 19:31:53.429: E/AndroidRuntime(6957): java.lang.RuntimeException: Error receiving broadcast
Intent { act=android.net.wifi.WIFI_STATE_CHANGED flg=0x4000010 (has extras) } in
com.example.wifi_socket_00.MainActivity$2@4302df40
11-07 19:31:53.429: E/AndroidRuntime(6957): at
android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:782)
11-07 19:31:53.429: E/AndroidRuntime(6957): at
com.example.wifi_socket_00.MainActivity$2.onReceive(MainActivity.java:78)