【问题标题】:Android service execution deniedAndroid服务执行被拒绝
【发布时间】:2013-06-13 15:19:17
【问题描述】:

我有一个服务实现为WakefulIntentService。每次向负责的BroadcastReceiver 发出适当的意图时,都会通过启动它来启动它。服务在两种情况下启动:在设备启动时和服务的运行实例即将完成时,并通过询问 Android 的常规任务调度程序 AlarmManager 在未来时间发出启动意图来安排新的执行。

问题是,出于安全原因,我have been advised不在Manifest文件中声明的服务中使用android:exported="true"。但是,省略它会导致在其中一部测试手机(运行 Android 4.1.2 的三星 S3)中拒绝执行服务:

06-13 11:34:34.181: W/ActivityManager(2270): Permission denied: checkComponentPermission() owningUid=10155
06-13 11:34:34.181: W/ActivityManager(2270): Permission Denial: Accessing service ComponentInfo{com.mypackage.myapp/com.mypackage.myapp.MyService} from pid=10320, uid=2000 that is not exported from uid 10155

添加android:exported="true" 可以解决问题。是否有替代方法可以在不影响应用安全性的情况下避免执行拒绝?

清单 xml 文件:

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

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

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

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

        <receiver android:name=".MyBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="com.mypackage.myapp" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.mypackage.myapp.MyService"
            android:exported="true">
        </service>

    </application>

</manifest>

BroadcastReceiver:

package com.mypackage.myapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.commonsware.cwac.wakeful.WakefulIntentService;

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        WakefulIntentService.sendWakefulWork(context, MyService.class);
    }
}

onDestroy()中包含启动意图调度代码的服务:

public class MyService extends WakefulIntentService {

(...)

    @Override
    public void doWakefulWork(Intent intent) {
        (...)
    }

    @Override
    public void onDestroy() {

        PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent("com.mypackage.myapp"), 0);

        AlarmManager am = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));

        am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, ONE_MINUTE, pi);

        super.onDestroy();
    }
}

【问题讨论】:

  • 那个错误没有多大意义。这是从引导还是从AlarmManager 发生的?知道另一个过程是什么(在您的错误中标识为10155 的过程)?顺便说一句,您不需要为您的BroadcastReceiver 使用操作字符串——只需使用new Intent(this, MyBroadcastReceiver.class)。此外,您不需要BOOT_COMPLETED &lt;intent-filter&gt; 上的类别,因为它没有被使用。
  • 10155 是提问者应用的 UID。 2000 是尝试发起者的 UID,这听起来像是一个平台组件,而不是一些随机的应用程序。
  • android 文档确实听起来像是需要导出服务,除非应该有一些未记录的系统起源异常。
  • @ChrisStratton:但是系统没有与服务对话,就像它与任何其他私人服务对话一样。 BOOT_COMPLETEDAlarmManager 都在与 BroadcastReceiver 对话,而不是服务。如果BroadcastReceiver 没有被导出,我肯定会看到一个问题。但是BroadcastReceiver 不需要导出Service,因为它在同一个应用程序中,默认情况下导出BroadcastReceiver,因为它有一个&lt;intent-filter&gt;
  • 从 logcat 中,显然有东西在试图与服务对话。可能是设置错误导致尝试进行广播而不是广播。

标签: android android-intent android-alarms commonsware-cwac


【解决方案1】:

实际上它不会在启动时发生,但是当我尝试在 adb shell 上手动启动它时:am startservice com.mypackage.myapp/.MyService.

那就别这样了。您的用户不会这样做。导出服务只是为了让您可以运行adb shell 命令,这并不是一个特别明智的举措。此外,您可以测试从adb shell发送开机广播,达到同样的目的,而且不必导出服务。

在我的 new Intent() 中删除操作字符串后,我无法在启动时启动服务

抱歉,我指的是您的第二个操作字符串。你的&lt;receiver&gt; 应该是这样的:

    <receiver android:name=".MyBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

对应的PendingIntent 将是:

PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(this, MyBroadcastReceiver.class), 0);

【讨论】:

  • 我也不喜欢这个想法,但我无法在不导出服务的情况下让服务服从启动时广播。在这种情况下有什么替代方案?此外,我仍在尝试使您建议的第二个接收器设置正常工作。作为旁注,我注意到 onDestroy() 方法不能保证完成,所以我不再建议在那里安排服务执行。
  • @Piovezan:“如果不导出服务,我无法让服务服从启动时广播”——它适用于所有其他开发人员。 “此外,我仍在尝试使您建议的第二个接收器设置正常工作”——我已经为您提供了一个项目链接,该项目演示了这个非导出服务 (github.com/commonsguy/cw-omnibus/tree/master/AlarmManager/… )。
  • 抱歉,我没注意你的链接。你的建议也有效,我错误地叫错了我的意图。 S3设备几天后将无法进行测试,但我已经验证您的项目在其中可以正常工作。我现在将我的项目与您的项目放在一起,以查看差异和我所缺少的。不过,唯一的区别似乎是接收者声明中缺少类别。
【解决方案2】:

我在 KitKat 上遇到了相同的症状(请参阅此错误:http://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=61850)。您可以尝试查看更改 Intent 的签名(例如操作)是否会产生影响。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 2016-04-15
    • 1970-01-01
    • 2011-02-02
    • 2019-06-01
    • 2017-06-01
    • 1970-01-01
    相关资源
    最近更新 更多