简体   繁体   English

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

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

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?如何解决缺少权限的问题?

Add the INTERNET permission 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 中的应用程序标记之外

In the latest release of Google Play, Google removed the need to ask permission for internet as "most apps need it anyways nowadays".在最新版本的 Google Play 中,Google 取消了请求互联网许可的需要,因为“现在大多数应用程序都需要它”。 However, for users who have older versions, it is still recommended to leave the code below in your manifest但是,对于旧版本的用户,仍然建议将下面的代码保留在您的清单中

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

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>

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

If you want using Internet in your app as well as check the network state ie Is app is connected to the internet then you have to use below code outside of the application tag.如果您想在您的应用程序中使用互联网并检查网络状态,即应用程序是否已连接到互联网,那么您必须在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>

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.就在应用程序标签上方。

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

I had the same problem even use <uses-permission android:name="android.permission.INTERNET" />我什至有同样的问题使用<uses-permission android:name="android.permission.INTERNET" />

If you want connect web api using http not https .如果您想使用http而不是https连接 web api。 Maybe you use android device using Android 9 (Pie) or API level 28 or higher .也许您使用的是 Android 9 (Pie) 或 API 级别 28 或更高版本的 android 设备。 android:usesCleartextTraffic default value is false . android:usesCleartextTraffic默认值为false You have to set be你必须设置为

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:usesCleartextTraffic="true" <!-- this line -->
        ...>
           ...
    </application>
</manifest>

Finally, should be https最后应该是https

https://developer.android.com/guide/topics/manifest/application-element#usesCleartextTraffic https://developer.android.com/guide/topics/manifest/application-element#usesCleartextTraffic

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

Just Add these 2 permissions只需添加这两个权限

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

in your app's AndroidManifest.xml在您应用的AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.android.networkusage"
        ...>
    
    
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
        <application 
            ...>
            ...
        </application>
    </manifest>

Happy Coding:)快乐编码:)

Just put below code in AndroidManifest :只需将以下代码放在 AndroidManifest 中:

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

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

To request for internet permission in your code you must add these to your AndroidManifest.xml file要在您的代码中请求互联网权限,您必须将这些添加到您的 AndroidManifest.xml 文件中

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

For more detail explanation goto https://developer.android.com/training/basics/network-ops/connecting有关更多详细说明,请转到https://developer.android.com/training/basics/network-ops/connecting

Google removed the need to ask permission for the internet for the latest version.谷歌取消了为最新版本请求互联网许可的需要。 Still, to request for internet permission in your code you must add these to your AndroidManifest.xml file.尽管如此,要在您的代码中请求互联网权限,您必须将这些添加到您的 AndroidManifest.xml 文件中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中打开网站需要设置上网权限吗? - Do I need to set Internet permission for opening a website in Android? 如果我有INTERNET,我是否需要ACCESS_NETWORK_STATE权限? - Do I need ACCESS_NETWORK_STATE permission if I have INTERNET one Android 6权限访问互联网 - Android 6 Permission access to internet Android:我需要许可吗? - Android: Do I need a permission? 我需要什么权限才能在 Android 上写入 USB 笔式驱动器? - What permission do I need to write to a usb pen drive on Android? Flutter 应用程序使用 url_launcher...我需要为 Android 添加 INTERNET 权限吗? - Flutter app using url_launcher…do I need to add the INTERNET permission for Android? Python:-如果即使屏幕锁定,我也想使android应用程序在后台运行。 我需要Android的哪些权限? - Python:- If i want to make an android app to run in background even if the screen is locked. What permission do i need for that from android? 出于什么原因我需要权限“android.permission.WRITE_OWNER_DATA” - For what reason do I need permission “android.permission.WRITE_OWNER_DATA” 我是否需要 Flutter 中的 Firebase/Google 移动广告的互联网许可 - Do I need the internet permission for Firebase/Google Mobile Ads in Flutter 我是否需要 Internet-permission 标签才能在应用程序中提供链接? - Do I need the Internet-permission tag to provide links in an app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM