Android 如何向应用程序添加清单权限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2169294/
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 04:47:29  来源:igfitidea点击:

How to add manifest permission to an application?

androidexceptionandroid-manifest

提问by rob

I am trying to access HTTP link using HttpURLConnectionin Android to download a file, but I am getting this warning in LogCat:

我正在尝试访问HttpURLConnection在 Android 中使用的 HTTP 链接以下载文件,但我在以下位置收到此警告LogCat

WARN/System.err(223): java.net.SocketException: Permission denied (maybe missing INTERNET permission)

WARN/System.err(223): java.net.SocketException: 权限被拒绝(可能缺少 INTERNET 权限)

I have added android.Manifest.permissionto my application but it's still giving the same exception.

我已经添加android.Manifest.permission到我的应用程序中,但它仍然给出相同的例外。

回答by Anthony Forloney

Assuming you do not have permissions set from your LogCaterror description, here is my contents for my AndroidManifest.xmlfile that has access to the internet:

假设您没有根据LogCat错误描述设置权限,以下是我AndroidManifest.xml可以访问 Internet 的文件的内容:

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

Other than that, you should be fine to download a file from the internet.

除此之外,您应该可以从互联网上下载文件。

回答by Marek Sebera

Permission name is CASE-SENSITIVE

权限名称区分大小写

In case somebody will struggle with same issue, it is case sensitivestatement, so wrong case means your application won't get the permission.

万一有人遇到同样的问题,它是区分大小写的语句,所以错误的大小写意味着您的应用程序将无法获得许可。

WRONG

错误的

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

CORRECT

正确的

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

This issue may happen ie. on autocomplete in IDE

这个问题可能会发生,即。在 IDE 中自动完成

回答by Teraiya Mayur

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.example.towntour.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Black.NoTitleBar" >
    <activity
        android:name="com.photoeffect.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>

</manifest>

回答by Chris

If you are using the Eclipse ADT plugin for your development, open AndroidManifest.xmlin the Android Manifest Editor (should be the default action for opening AndroidManifest.xmlfrom the project files list).

如果您使用 Eclipse ADT 插件进行开发,请AndroidManifest.xml在 Android 清单编辑器中打开(应该是AndroidManifest.xml从项目文件列表中打开的默认操作)。

Afterwards, select the Permissionstab along the bottom of the editor (Manifest - Application - Permissions - Instrumentation - AndroidManifest.xml), then click Add...a Uses Permissionand select the desired permission from the dropdown on the right, or just copy-paste in the necessary one (such as the android.permission.INTERNETpermission you required).

然后,选择Permissions编辑器底部的选项卡 ( Manifest - Application - Permissions - Instrumentation - AndroidManifest.xml),然后单击Add...aUses Permission并从右侧的下拉列表中选择所需的权限,或者只需复制粘贴必要的android.permission.INTERNET权限(例如您需要的权限)。

回答by nitesh

Copy the following line to your application manifest file and paste before the <application>tag.

将以下行复制到您的应用程序清单文件并粘贴到<application>标记之前。

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

Placing the permission below the <application/>tag will work, but will give you warning. So take care to place it before the <application/>tag declaration.

将权限放在<application/>标签下方会起作用,但会给您警告。所以要注意把它放在<application/>标签声明之前。

回答by Mathayo

When using eclipse, Follow these steps

使用eclipse时,请按照以下步骤操作

1) Double click on the manifest to show it on the editor
2) Click on the permissions tab below the manifest editor
3) Click on Add button
4) on the dialog that appears Click uses permission. (Ussually the last item on the list)
5) Notice the view that appears on the rigth side Select "android.permission.INTERNET"
6) Then a series of Ok and finally save.

Hope this helps

1) 双击清单以在编辑器上显示它
2) 单击清单编辑器下方的权限选项卡
3) 单击添加按钮
4) 在出现的对话框中单击使用权限。(通常是列表中的最后一项)
5)注意右边出现的视图选择“android.permission.INTERNET”
6)然后一系列的Ok,最后保存。

希望这可以帮助

回答by Khemraj

I am late but i want to complete the answer.

我迟到了,但我想完成答案。

An permission is added in manifest.xmllike

一个权限被添加在manifest.xml

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

This is enough for standard permissionswhere no permission is prompted to the user. However, it is not enough to add permission only to manifest if it is a dangerous permission. See android doc. Like Camera, Storage permissions.

这对于没有提示用户权限的标准权限来说已经足够了。但是,如果是危险权限,仅添加权限是不够的。请参阅android 文档。像相机,存储权限。

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

You will need to ask permission from user. I use RxPermissionlibrary that is widely used library for asking permission. Because it is long code which we have to write to ask permission.

您需要征得用户的许可。我使用RxPermission库,这是一个广泛使用的库来请求许可。因为它是很长的代码,我们必须编写以获得许可。

RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity instance // Must be done during an initialization phase like onCreate
rxPermissions
    .request(Manifest.permission.CAMERA)
    .subscribe(granted -> {
        if (granted) { // Always true pre-M
           // I can control the camera now
        } else {
           // Oups permission denied
        }
    });

Add this library to your app

将此库添加到您的应用

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.tbruyelle:rxpermissions:0.10.1'
    implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'
}

回答by lenrok258

That may be also interesting in context of adding INTERNET permission to your application:

在向应用程序添加 INTERNET 权限的上下文中,这也可能很有趣:

Google has also given each app Internet access, effectively removing the Internet access permission. Oh, sure, Android developers still have to declare they want Internet access when putting together the app. But users can no longer see the Internet access permission when installing an app and current apps that don't have Internet access can now gain Internet access with an automatic update without prompting you.

谷歌还给了每个应用上网权限,有效地取消了上网权限。哦,当然,Android 开发人员在组合应用程序时仍然必须声明他们想要访问互联网。但是用户在安装应用程序时无法再看到 Internet 访问权限,当前没有 Internet 访问权限的应用程序现在可以通过自动更新获得 Internet 访问权限,而无需提示您。

Source: http://www.howtogeek.com/190863/androids-app-permissions-were-just-simplified-now-theyre-much-less-secure/

资料来源:http: //www.howtogeek.com/190863/androids-app-permissions-were-just-simplified-now-theyre-much-less-secure/

Bottom line is that you still have to add INTERNET permission in manifest file but application will be updated on user's devices without asking them for new permission.

最重要的是,您仍然必须在清单文件中添加 INTERNET 权限,但应用程序将在用户设备上更新,而不会要求他们提供新权限。

回答by zemunkh

You have to use both Network and Access Network State in manifest file while you are trying load or access to the internet through android emulator.

当您尝试通过 android 模拟器加载或访问互联网时,您必须在清单文件中同时使用网络和访问网络状态。

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

If you are giving only .INTERNET permission, it won't access to the internet.

如果您只授予 .INTERNET 权限,它将无法访问互联网。