0

As the title says, I can't get State in Broadcastreceiver from NetWorkInfo Class.

It returns some errors in log output which are given below.

I need your help.

Here is the log output and the Broacastreceiver class , I also notice that I've added all permission (Access Network State et Internet ) and the Broadcast intent action android:name="android.net.conn.CONNECTIVITY_CHANGE.

So I dare say there isn't problem in manifest.xml file .

ConnectivityManager cm =
            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

    State state = activeNetwork.getState();

  switch (state) {

  case CONNECTING:
      Toast.makeText(context, "connecting", Toast.LENGTH_SHORT).show();
  break;

and the log output is:

java.lang.RuntimeException: Unable to start receiver com.example.test.ConnectivityChangeReceiver: java.lang.NullPointerException
E/AndroidRuntime( 9873):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2668)
E/AndroidRuntime( 9873):    at android.app.ActivityThread.access$1800(ActivityThread.java:172)
E/AndroidRuntime( 9873):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1384)
E/AndroidRuntime( 9873):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 9873):    at android.os.Looper.loop(Looper.java:146)
E/AndroidRuntime( 9873):    at android.app.ActivityThread.main(ActivityThread.java:5653)
E/AndroidRuntime( 9873):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 9873):    at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime( 9873):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
E/AndroidRuntime( 9873):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
E/AndroidRuntime( 9873):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 9873): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 9873):    at com.example.test.ConnectivityChangeReceiver.onReceive(ConnectivityChangeReceiver.java:27)
E/AndroidRuntime( 9873):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2653)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Tete Tete
  • 23
  • 3

2 Answers2

1

You are getting NPE on this line activeNetwork.getState() and it is because activeNetwork is null. This may return null when there is no default network.

Make sure there is Active Network when you are testing this app. In that case activeNetwork.getState() should be non-null.

If your objective is to test network connectivity for Wi-Fi and mobile, then you may try using below code:

ConnectivityManager cm =
                (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
        boolean isWifiConn = networkInfo.isConnected();
        networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        boolean isMobileConn = networkInfo.isConnected();


        Log.d("ConnectivityChangeReceiver", "Wifi connected: " + isWifiConn);
        Log.d("ConnectivityChangeReceiver", "Mobile connected: " + isMobileConn);

This code determines whether these network interfaces are available (that is, whether network connectivity is possible) and/or connected (that is, whether network connectivity exists and if it is possible to establish sockets and pass data.

Also, you should not base decisions on whether a network is "available." You should always check isConnected() before performing network operations, since isConnected() handles cases like flaky mobile networks, airplane mode, and restricted background data.

public boolean isOnline() {
    ConnectivityManager connMgr = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    return (networkInfo != null && networkInfo.isConnected());
} 
AADProgramming
  • 6,077
  • 11
  • 38
  • 58
0

I believe you are missing the following permissions in your manifest.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<uses-permission android:name="android.permission.INTERNET"/>
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428