Android ware链接不上,Android服务执行被拒绝

我有一个实现为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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值