我有一个实现为WakefulIntentService的服务。每次BroadcastReceiver
通过启动它向负责人发出适当的意图时都会启动它。该服务在两种情况下启动:在设备启动时以及当服务的运行实例即将完成时,并通过要求 Android 的传统任务调度程序 ,AlarmManager
来安排新的执行,以在未来某个时间发出启动器意图。
问题是,出于安全原因,我被建议不要android:exported="true"
在 Manifest 文件中声明的服务中使用。但是,省略它会导致在其中一部测试手机(运行 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();
}
}