Reputation: 189554
I work on a library that does some tricks if the user is not online. For this I request the network status like this:
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
boolean isConnected = !(info == null || !info.isConnected());
How can I only do this check if the permission
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
is available?
I don't want to force all downstream projects of the lib to require this permission only for the tricks in the library.
Upvotes: 0
Views: 67
Reputation: 1543
I think you should use checkPermission utility.
int res = getContext().checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);
return (res == PackageManager.PERMISSION_GRANTED);
Upvotes: 1