Java 一个 Android 服务可以有多个权限吗?

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

Can an Android Service have multiple Permissions?

javaandroidpermissionsmanifest

提问by Aymon Fournier

I have a service which both downloads a file and saves it, and it seems I can only specify one permission.

我有一项既下载文件又保存文件的服务,看来我只能指定一个权限。

<service android:enabled="true"
            android:name=".DownloadService" android:permission="android.permission.INTERNET">            
</service>

or

或者

<service android:enabled="true"
            android:name=".DownloadService" android:permission="android.permission.WRITE_EXTERNAL_STORAGE">            
</service>

I need both.

我两个都需要。

采纳答案by Dave Webb

An Android Servicecan have multiple permissions, but permissions are granted at the application level, not at the Servicelevel.

一个 AndroidService可以有多个权限,但权限是在应用程序级别授予的,而不是在Service级别。

Your problem is that you are trying to grant permission to your Servicein the wrong place in the AndroidManifest.xmlfile.

您的问题是您试图ServiceAndroidManifest.xml文件中的错误位置授予您的权限。

The android:permissionattribute of a Servicespecifies the permission that an entity must have to use the service, not a permission granted to the Service.

a 的android:permission属性Service指定实体必须拥有使用服务的权限,而不是授予 Service.

You grant permissions to all components your applicationat the top level of your AndroidManifest.xml:

您在的顶级应用程序授予对所有组件的权限AndroidManifest.xml

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.yourdomain.yourapp" >

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

</manifest>