Is the request for internet persmission required at runtime (Android)?

你说的曾经没有我的故事 提交于 2019-12-08 17:30:35

问题


For Android, it is required that we ask permissions at runtime to make sure users understand better why en when permissions are needed. I know this is true for permissions like WRITE_CALENDAR and ACCESS_FINE_LOCATION but it seems it's not required for INTERNET. Not strange because almost all apps use internet.

Is it safe to say that I only need to declare it in the manifest?

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

Or should I always check it at runtime?


回答1:


No, you shouldn't ask for INTERNET permission at runtime.

INTERNET belongs to the Normal permissions group, which are automatically granted by the system if they're declared in the Manifest, as mentioned in this document:

Normal permissions do not directly risk the user's privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically.




回答2:


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

Your permission is right but you have to check internet connectivity before using any internet related function . You can check internet connected or not by following function 


public static boolean isNetworkOnline(Context con)
    {
        boolean status = false;
        try 
        {
            ConnectivityManager cm = (ConnectivityManager) con
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getNetworkInfo(0);

            if (netInfo != null && netInfo.getState() == State.CONNECTED) {
                status = true;
            } else {
                netInfo = cm.getNetworkInfo(1);

                if (netInfo != null && netInfo.getState() == State.CONNECTED) {
                    status = true;
                } else {
                    status = false;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return status;
    }



回答3:


Internet permissions work as pre-sdk 23 permissions. Permission is given at installation of the app.

INTERNET permissions are regarded as PROTECTION_NORMAL.

If an app declares in its manifest that it needs a normal permission, the system automatically grants the app that permission at install time. The system does not prompt the user to grant normal permissions, and users cannot revoke these permissions.

Dangerous permission require runtime permission management. They are also in 'permission groups' so once runtime permission is granted for one permission from that group, it does not need to be granted for other permissions from the same group.

Also permssions can be granted at runtime and set as default acceptance, which can also be revoked at any time by the user.




回答4:


By default it is not required. only use it when you need internet connectivity in your app.



来源:https://stackoverflow.com/questions/34435639/is-the-request-for-internet-persmission-required-at-runtime-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!