尝试使用broadCastReceiver
测试ConnectivityManager.CONNECTIVITY_ACTION
时,应用程序崩溃,logcat
说Error receiving broadcast Intent
。请让我知道我错过了什么。
代码:
public class wifi_socket_01 extends Activity {
TextView tv_conn_status;
TextView tv_conn_ssid;
TextView tv_conn_speed;
TextView tv_conn_strengt;
TextView tv_conn_ip;
EditText et_ip;
ToggleButton tobBtn_connect;
EditText et_msg;
Button btn_send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SetUpAllViews(R.layout.activity_main);
displayWiFiStates();
this.registerReceiver(this.myWiFiReceiver,
new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
private BroadcastReceiver myWiFiReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_EXTRA_INFO);
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
displayWiFiStates();
}
}
};
private void SetUpAllViews(int layout) {
// TODO Auto-generated method stub
setContentView(layout);
tv_conn_status = (TextView) findViewById(R.id.tv_conn_status);
tv_conn_ssid = (TextView) findViewById(R.id.tv_conn_ssid);
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);
tobBtn_connect = (ToggleButton) findViewById(R.id.togbtn_connect);
et_msg = (EditText) findViewById(R.id.et_msg);
btn_send = (Button) findViewById(R.id.btn_send);
}
protected void displayWiFiStates() {
// TODO Auto-generated method stub
String service = Context.CONNECTIVITY_SERVICE;
ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(service);
NetworkInfo myNetworkInfo = myConnManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
WifiManager myWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo myWiFiInfo = myWifiManager.getConnectionInfo();
if(myNetworkInfo.isConnected()) {
tv_conn_ip.setText("IP: "+myWiFiInfo.getIpAddress());
}else {
tv_conn_status.setText("Disconnected");
}
}
}
logcat的:
11-08 10:26:57.645: E/AndroidRuntime(15348): FATAL EXCEPTION: main
11-08 10:26:57.645: E/AndroidRuntime(15348): Process: com.example.wifi_socket_01, PID:
15348
11-08 10:26:57.645: E/AndroidRuntime(15348): java.lang.RuntimeException: Error
receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010
(has extras) } in com.example.wifi_socket_01.MainActivity$1@4302f638
11-08 10:26:57.645: E/AndroidRuntime(15348): at
android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:782)
11-08 10:26:57.645: E/AndroidRuntime(15348): at a
ndroid.os.Handler.handleCallback(Handler.java:733)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
android.os.Handler.dispatchMessage(Handler.java:95)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
android.os.Looper.loop(Looper.java:157)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
android.app.ActivityThread.main(ActivityThread.java:5293)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
java.lang.reflect.Method.invokeNative(Native Method)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
java.lang.reflect.Method.invoke(Method.java:515)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
dalvik.system.NativeStart.main(Native Method)
11-08 10:26:57.645: E/AndroidRuntime(15348): Caused by: java.lang.NullPointerException
11-08 10:26:57.645: E/AndroidRuntime(15348): at
com.example.wifi_socket_01.MainActivity$1.onReceive(MainActivity.java:48)
11-08 10:26:57.645: E/AndroidRuntime(15348): at
android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:772)
答案 0 :(得分:2)
试试这个:
改变你的
NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_EXTRA_INFO);
到
ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
来自文档: EXTRA_EXTRA_INFO
提供可选提供的额外字符串的字符串的查找键 有关网络状态的信息。
提供可选提供的额外字符串的字符串的查找键 有关网络状态的信息。信息可能会被传递 来自较低的网络层,其含义可能是特定于a 特定的网络类型。使用getStringExtra(String)检索它。
常数值:" extraInfo"
所以我们找到了问题根源的解释,它实际上从未实际返回networkInfo
。它完全与网络状态有关。
希望它有所帮助:)
答案 1 :(得分:1)
您可能在AndroidManifest中错过了正确的Android权限:
def avgs = sums.collect{patient, value -> (value / counts[patient]) }
答案 2 :(得分:0)
我遇到了同样的问题,结果是onReceive函数中的空指针,我通过改变来修复它
Boolean up = cm.getActiveNetworkInfo().isConnected();
到
NetworkInfo info = cm.getActiveNetworkInfo();
boolean up = false;
if(info != null && info.isConnected()){
up = true;
}
我也在Android N上,这是在UI线程上运行的服务上发生的