0

I need to know network type, but it requires permissions.

I'm trying this way:

public String getConnectionTypeNew(Context context){
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null) { // connected to the internet
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            return "WiFi Network";
        } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
            return "Mobile Network";
        }
    }
    return "No Network";
}

Is there a way to get network type without requiring permissions?

halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • ACCESS_NETWORK_STATE is not a dangerous permission. Why are you trying to bypass it? – DeeV Oct 04 '16 at 21:14

1 Answers1

0

Are you setting the permissions in the manifest?

If not put these in your manifest.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Lucas Senechal
  • 300
  • 1
  • 3
  • 15