从 Android 应用程序访问 Internet 需要什么权限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2378607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:36:45  来源:igfitidea点击:

What permission do I need to access Internet from an Android application?

androidandroid-permissions

提问by Janusz

I get the following Exception running my app:

运行我的应用程序时出现以下异常:

java.net.SocketException: Permission denied (maybe missing INTERNET permission)

How do I solve the missing permission problem?

如何解决缺少权限的问题?

回答by Nikola Smiljani?

Add the INTERNETpermission to your manifest file.

INTERNET权限添加到您的清单文件。

You have to add this line:

你必须添加这一行:

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

outside the application tag in your AndroidManifest.xml

在 AndroidManifest.xml 中的应用程序标记之外

回答by Finley Smith

In the latest release of Google Play, Google removed the need to ask permission for internet as "most apps need it anyways nowadays". However, for users who have older versions, it is still recommended to leave the code below in your manifest

在最新版本的 Google Play 中,Google 取消了请求互联网许可的需要,因为“现在大多数应用程序都需要它”。但是,对于旧版本的用户,仍然建议将下面的代码保留在您的清单中

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

回答by Kirtikumar A.

just put above line like below

就像下面一样放在上面

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.avocats.activeavocats"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="16" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >


    <activity
        android:name="com.example.exp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

回答by AndroidDev

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

回答by Amy

If you want using Internet in your app as well as check the network state i.e. Is app is connected to the internet then you have to use below code outside of the applicationtag.

如果您想在您的应用程序中使用互联网并检查网络状态,即应用程序是否已连接到互联网,那么您必须在application标签之外使用以下代码。

For Internet Permission:

互联网许可:

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

For Access network state:

对于接入网络状态:

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

Complete Code:

完整代码:

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="16" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >


    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

回答by Akshay Paliwal

if just using internet then use-

如果只是使用互联网然后使用-

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

if you are getting the state of internet then use also -

如果您正在获得互联网状态,那么也可以使用 -

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

just above the application tag.

就在应用程序标签上方。

回答by Vaibhav Joshi

forget about adding the permission into the manifest Add this code as a method

忘记将权限添加到清单中 将此代码添加为方法

public static boolean hasPermissions(Context context, String... permissions)
{
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null)
    {
        for (String permission : permissions)
        {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED)
            {
                return false;
            }
        }
    }
    return true;
}

and write this in your Main

并将其写入您的Main

int PERMISSION_ALL = 1;
    String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_SMS, Manifest.permission.CAMERA};

    if (!hasPermissions(this, PERMISSIONS)) {
        ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
    }

回答by Olorunfemi Ajibulu

Use these:

使用这些:

<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" />

回答by Hiren Gondaliya

Just put below code in AndroidManifest :

只需将以下代码放在 AndroidManifest 中:

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

回答by Kashif Faraz Shamsi

As per current versions, Android doesn't ask for permission to interact with the internet but you can add the below code which will help for users using older versions Just add these in AndroidManifest

根据当前版本,Android 不要求与互联网交互的许可,但您可以添加以下代码,这将有助于使用旧版本的用户只需在 AndroidManifest 中添加这些代码

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