是否需要在运行时请求互联网许可 (Android)?

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

对于Android,我们要求在运行时请求权限,以确保用户更好地理解为什么需要权限。我知道这对于像 WRITE_CALENDARACCESS_FINE_LOCATION 这样的权限是正确的,但它似乎不需要互联网。这并不奇怪,因为几乎所有应用程序都使用互联网。

可以说我只需要在清单中声明它吗?

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

或者我应该总是在运行时检查它吗?

不,您不应该在运行时请求 INTERNET 权限。

INTERNET属于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.

<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;
    }

互联网权限作为 pre-sdk 23 权限。在安装应用程序时授予权限。

互联网权限被视为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.

危险权限需要运行时权限管理。它们也在 'permission groups' 中,因此一旦为该组中的一个权限授予了运行时权限,就不需要为同一组中的其他权限授予它。

也可以在运行时授予权限并设置为默认接受,也可以由用户随时撤销。

默认情况下不需要。仅当您的应用程序需要互联网连接时才使用它。