When trying to test the broadCastReceiver
with the ConnectivityManager.CONNECTIVITY_ACTION
, the App crashes and logcat
says Error receiving broadcast Intent
. please let me know what i am missing.
Code :
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)
try this instead:
change your
NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_EXTRA_INFO);
to
ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
from the docs: EXTRA_EXTRA_INFO
The lookup key for a string that provides optionally supplied extra information about the network state.
The lookup key for a string that provides optionally supplied extra information about the network state. The information may be passed up from the lower networking layers, and its meaning may be specific to a particular network type. Retrieve it with getStringExtra(String).
Constant Value: "extraInfo"
so we found the explanation of the root of the problem, it never actually returns networkInfo
afterall. Its all about the network state.
hope it helps :)
You are probably missing the correct Android permissions in the AndroidManifest:
<!-- ANDROID PERMISSIONS -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
I ran into the same issue, it turned out to be a null pointer inside the onReceive function, I fixed this by altering
Boolean up = cm.getActiveNetworkInfo().isConnected();
into
NetworkInfo info = cm.getActiveNetworkInfo();
boolean up = false;
if(info != null && info.isConnected()){
up = true;
}
I'm also on Android N, this is happening on a service that runs on the UI thread
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.