我有一个实现为WakefulIntentService的服务.每次启动适当的意图给负责的BroadcastReceiver时,就会启动该服务.该服务在两种情况下启动:在设备启动时以及该服务的运行实例即将完成时,并通过要求Android的常规任务计划程序AlarmManager在将来的时间发出启动程序意图来计划新的执行.
问题是,出于安全原因,我have been advised不要在清单文件中声明的服务中使用android:exported =“ true”.但是,如果忽略它,则会导致其中一部测试手机(运行Android 4.1.2的Samsung 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文件:
package="com.mypackage.myapp"
android:versionCode="3"
android:versionName="1.0">
android:minSdkVersion="8"
android:targetSdkVersion="17" />
android:allowBackup="true"
android:icon="@drawable/ic"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.mypackage.myapp.MyService"
android:exported="true">
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();
}
}
解决方法:
Actually it doesn’t occur from boot but when I attempt to start it manually on adb shell: am startservice com.mypackage.myapp/.MyService.
那不要那样做您的用户不会这样做.导出服务只是为了您可以运行adb shell命令,这并不是一个特别明智的举动.此外,您可以测试从adb shell发送启动时广播,达到相同的目的,而不必导出服务.
I haven’t been able to make the service start upon boot after removing the action string in my new Intent()
抱歉,我的意思是您的第二个动作字符串.您的应该看起来像:
相应的PendingIntent将是:
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(this, MyBroadcastReceiver.class), 0);
标签:android-intent,android-alarms,commonsware-cwac,android
来源: https://codeday.me/bug/20191030/1970518.html