Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need ACCESS_NETWORK_STATE permission if I have INTERNET one

Tags:

android

I had an INTERNET permission in my app. But now I want to add google analytics in my app. And it says it need a CHECK_INTERNET_SATE. Do I have to add it? If so, it looks very strange - app, which can access internet can't read it's state. I don't think it's normal

like image 665
kandi Avatar asked May 20 '14 07:05

kandi


People also ask

What is android permission ACCESS_NETWORK_STATE for?

the ACCESS_NETWORK_STATE permission is used to avoid using the internet when it isn't available.

Which permission is required for used Internet?

Note: Both the Internet and ACCESS_NETWORK_STATE permissions are normal permissions, which means they're granted at install time and don't need to be requested at runtime.


1 Answers

There is a basic difference between these 2 permissions,

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

The above permission Allows applications to open network sockets.

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

Where as above ACCESS_NETWORK_STATE permission Allows applications to access information about networks.

Example:

If you want to load a URL in a WebView, you only need android.permission.INTERNET permission.

If there is a situation where you have to download some data from a server, you need to check internet connection availability before downloading. Otherwise app may crash if there is no connectivity.Like following ways, you can check internet connectivity,

public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

In the above snippet, you are fetching information about the network so here you have to take android.permission.ACCESS_NETWORK_STATE permission.

And it says it need a CHECK_INTERNET_SATE. Do I have to add it?

Google Analytics SDK checks network availability before sending information to the server so you need that permission too.

Hope it clears your doubt.

reference

http://developer.android.com/reference/android/Manifest.permission.html

https://stackoverflow.com/a/19642087/1665507

like image 199
Spring Breaker Avatar answered Sep 16 '22 14:09

Spring Breaker