Android服务执行被拒绝

Android服务执行被拒绝,第1张

概述我有一个实现为WakefulIntentService的服务.每次启动适当的意图给负责的BroadcastReceiver时,就会启动该服务.该服务在两种情况下启动:在设备启动时以及该服务的运行实例即将完成时,并通过要求Android的常规任务计划程序AlarmManager在将来的时间发出启动程序意图来计划新的执行.问

我有一个实现为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=1015506-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();    }}

解决方法:

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()

抱歉,我的意思是您的第二个动作字符串.您的<接收者>应该看起来像:

    <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);

总结

以上是内存溢出为你收集整理的Android服务执行被拒绝全部内容,希望文章能够帮你解决Android服务执行被拒绝所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1086666.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存