点击broadcastReceivers
时,我onResume
注册了toggle button
个onPause
。在logcat
我取消注册它们,但当我按下主页按钮时,应用程序崩溃@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SetUpAllViews(R.layout.activity_main);
}
private void setViewState(int view, boolean state) {
// TODO Auto-generated method stub
switch(view) {
case R.id.togbtn_connect: tog_connect.setEnabled(state);
case R.id.et_ip: et_ip.setEnabled(state);
case R.id.btn_send: btn_send.setEnabled(state);
case R.id.et_msg: et_msg.setEnabled(state);
}
}
private BroadcastReceiver mNetworkReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
mConnMgr = (ConnectivityManager)getSystemService(network_service);
networkInfo = mConnMgr.getActiveNetworkInfo();
if (networkInfo != null) {
if (networkInfo.isConnected()) {
setText(R.id.tv_conn_status, ""+networkInfo);
registerReceiver(mWiFiReceiver,
new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
setViewState(R.id.et_ip, true);
setViewState(R.id.btn_send, false);
setViewState(R.id.et_msg, false);
}else if (networkInfo.isConnectedOrConnecting()) {
setText(R.id.tv_conn_status, "Status: CONNECTED Or CONNECTING");
}else if (networkInfo.isFailover()){
setText(R.id.tv_conn_status, "Status: FailOver");
}else if (networkInfo.isRoaming()) {
setText(R.id.tv_conn_status, "Status: Roaming");
}else if (networkInfo.isAvailable()) {
setText(R.id.tv_conn_status, "Status: Available");
}
}else {
setText(R.id.tv_conn_status, "Status: No Default NetWork Connected");
}
}
};
private BroadcastReceiver mWiFiReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
mWiFi_Mgr = (WifiManager) getSystemService(wifi_service);
if (mWiFi_Mgr != null)
mWiFi_Info = mWiFi_Mgr.getConnectionInfo();
int wifiExtras = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
switch(wifiExtras) {
case WifiManager.WIFI_STATE_ENABLING: setText(R.id.tv_wifi_state, "Enabling..."); break;
case WifiManager.WIFI_STATE_ENABLED:
setText(R.id.tv_wifi_state, "Enabled");
if (mWiFi_Info != null) {
if (mWiFi_Info.getBSSID() != null) {
setText(R.id.tv_conn_bssid, "BSSID: "+mWiFi_Info.getBSSID());
}else {
setText(R.id.tv_conn_bssid, "BSSID: "+mWiFi_Info.getBSSID());
}
setText(R.id.tv_conn_speed, "Speed: "+mWiFi_Info.getLinkSpeed()+WifiInfo.LINK_SPEED_UNITS);
setText(R.id.tv_conn_strength, "RSSI: "+WifiManager.calculateSignalLevel(mWiFi_Info.getRssi(), 5));
setText(R.id.tv_conn_ip, getIPv4(mWiFi_Info.getIpAddress()));
}
break;
case WifiManager.WIFI_STATE_DISABLING: setText(R.id.tv_wifi_state, "Disabling..."); break;
case WifiManager.WIFI_STATE_DISABLED: setText(R.id.tv_wifi_state, "Disabled"); break;
case WifiManager.WIFI_STATE_UNKNOWN: setText(R.id.tv_wifi_state, "Unkown_WiFi_State"); break;
}
}
};
private void SetUpAllViews(int layout) {
// TODO Auto-generated method stub
setContentView(layout);
tv_conn_status = (TextView) findViewById(R.id.tv_conn_status);
tv_wifi_state = (TextView) findViewById(R.id.tv_wifi_state);
tv_conn_bssid = (TextView) findViewById(R.id.tv_conn_bssid);
tv_conn_speed = (TextView) findViewById(R.id.tv_conn_speed);
tv_conn_strengt = (TextView) findViewById(R.id.tv_conn_strength);
tv_conn_ip = (TextView) findViewById(R.id.tv_conn_ip);
et_ip = (EditText) findViewById(R.id.et_ip);
tog_connect = (ToggleButton) findViewById(R.id.togbtn_connect);
et_msg = (EditText) findViewById(R.id.et_msg);
btn_send = (Button) findViewById(R.id.btn_send);
}
private void setText(int view, String text) {
// TODO Auto-generated method stub
switch(view) {
case R.id.tv_conn_status: tv_conn_status.setText(text); break;
case R.id.tv_wifi_state: tv_wifi_state.setText(text); break;
case R.id.tv_conn_bssid: tv_conn_bssid.setText(text); break;
case R.id.tv_conn_speed: tv_conn_speed.setText(text); break;
case R.id.tv_conn_strength: tv_conn_strengt.setText(text); break;
case R.id.tv_conn_ip: tv_conn_ip.setText(text); break;
}
}
private String getIPv4(int ip) {
// TODO Auto-generated method stub
String result = String.format("%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff),
(ip >> 24 & 0xff));
return result;
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
tog_connect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
registerReceiver(mNetworkReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}else {
if (mWiFi_Mgr.getWifiState() == WifiManager.WIFI_STATE_ENABLED)
mWiFi_Mgr.setWifiEnabled(false);
if (mWiFiReceiver != null)
unregisterReceiver(mWiFiReceiver);
if (mNetworkReceiver != null)
unregisterReceiver(mNetworkReceiver);
}
}
});
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//if (mWiFiReceiver != null)
//unregisterReceiver(mWiFiReceiver);
if (mNetworkReceiver != null)
unregisterReceiver(mNetworkReceiver);
}
会产生以下错误。
Code_Updated_2 :
11-08 17:18:00.025: E/AndroidRuntime(5541): FATAL EXCEPTION: main
11-08 17:18:00.025: E/AndroidRuntime(5541): Process: com.example.wifi_socket_01, PID:
5541
11-08 17:18:00.025: E/AndroidRuntime(5541): java.lang.RuntimeException: Unable to pause
activity {com.example.wifi_socket_01/com.example.wifi_socket_01.wifi_socket_01}:
java.lang.IllegalArgumentException: Receiver not registered:
com.example.wifi_socket_01.wifi_socket_01$1@43033000
11-08 17:18:00.025: E/AndroidRuntime(5541): Caused by:
java.lang.IllegalArgumentException: Receiver not registered: com.
example.wifi_socket_01.wifi_socket_01$1@43033000
11-08 17:18:00.025: E/AndroidRuntime(5541): at
android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:671)
11-08 17:18:00.025: E/AndroidRuntime(5541): at
android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1785)
11-08 17:18:00.025: E/AndroidRuntime(5541): at
com.example.wifi_socket_01.wifi_socket_01.onPause(wifi_socket_01.java:184)
}
logcat的:
{{1}}
答案 0 :(得分:1)
onResume()您只注册一个接收器。但mWiFiReceiver从另一个接收器注册。 当onPause执行时,它将自然地取消注册mNetworkReceiver。但unregisterReceiver(mWiFiReceiver);给你错误,因为它没有在那里注册。
答案 1 :(得分:1)
或者你可以在onPause()方法中包含一个try {...} catch(IllegalArgumentException e){...}结构。
会是这样的:
if (mWiFi_Mgr.getWifiState() == WifiManager.WIFI_STATE_ENABLED)
mWiFi_Mgr.setWifiEnabled(false);
if (mWiFiReceiver != null)
try {
unregisterReceiver(mWiFiReceiver);
} catch(IllegalArgumentException e) {
//your catching code.
} catch (Exception e) {
//just in case other exception appears
}
....
您将能够捕捉到未解决的问题。
答案 2 :(得分:1)
例外情况You are attempting to unregister the receiver which is not registered
在您onResume()
中只有一个接收者正在注册,但在onPause()
中有两个接收者未注册。尝试取消注册not-registered
接收器(wifi接收器)导致引发异常。