Android 摆脱“导出的服务不需要许可”警告

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

Get rid of "Exported service does not require permission" warning

androidandroid-activitywarningsandroid-manifestandroid-service

提问by rekire

I'm looking for a solution to get rid of the warning. I don't understand even why it appears. I took a look at a SDK example where no warning appears.

我正在寻找消除警告的解决方案。我什至不明白它为什么会出现。我查看了一个没有出现警告的 SDK 示例。

At first here is my manifest where I get the warning Exported service does not require permission:

首先,这是我的清单,我收到警告导出服务不需要许可

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

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

    <uses-feature android:name="android.hardware.camera"
                  android:required="true" />

    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
    <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock">
        <service
            android:name=".AuthenticationService"
            android:exported="true">
            <intent-filter>
                <action
                    android:name="android.accounts.AccountAuthenticator" />
            </intent-filter>
            <meta-data
                android:name="android.accounts.AccountAuthenticator"
                android:resource="@xml/authenticator" />
        </service>

        <activity
            android:name=".Test"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
...

While the SampleSyncAdapterof the Android SDK has this manifest:

虽然Android SDK的SampleSyncAdapter有这个清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.samplesync"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <!-- ... -->

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

    <application
        android:icon="@drawable/icon"
        android:label="@string/label">
        <!-- The authenticator service -->
        <service
            android:name=".authenticator.AuthenticationService"
            android:exported="true">
            <intent-filter>
                <action
                    android:name="android.accounts.AccountAuthenticator" />
            </intent-filter>
            <meta-data
                android:name="android.accounts.AccountAuthenticator"
                android:resource="@xml/authenticator" />
        </service>
    ...

But there is no warning. Why the hell do I get a warning? Well I use the Theme.Sherlock theme for the usage of the ActionBarSherlock. But I cannot imagine that this causes the error.

但是没有警告。为什么我会收到警告?好吧,我使用 Theme.Sherlock 主题来使用ActionBarSherlock。但我无法想象这会导致错误。

回答by David Wasser

The warning is telling you that you have exported (ie: made publicly available) a service without securing it with a permission. This makes your service available to any other applications without restrictions. See Exported service does not require permission: what does it mean?

该警告告诉您,您在未经许可保护的情况下导出(即:公开提供)服务。这使您的服务可无限制地用于任何其他应用程序。请参阅导出的服务不需要许可:这是什么意思?

If your service doesn't need to be available to other applications, you don't need to export it. Explicitly setting android:exported="false"will remove the warning.

如果您的服务不需要对其他应用程序可用,则不需要导出它。显式设置android:exported="false"将删除警告。

Note:The default value for android:exportedis trueif you have provided an Intent filter.

注意:默认值android:exportedtrue如果您提供了意图过滤器。

回答by Paul de Vrieze

You need to add a permission requirement for your service. The one you need is android.permission.ACCOUNT_MANAGER. Your authenticator is only accessed through the manager.

您需要为您的服务添加权限要求。你需要的是android.permission.ACCOUNT_MANAGER. 您的身份验证器只能通过管理器访问。

回答by Juan Saravia

Regarding this issue, there is already a bug created in Android project about this warning using "Sync Adapter": https://code.google.com/p/android/issues/detail?id=37280but it's still an Open bug.

关于这个问题,Android 项目中已经使用“同步适配器”创建了一个关于此警告的错误:https: //code.google.com/p/android/issues/detail?id=37280但它仍然是一个开放错误。