【问题标题】:Android - Trying to test a service on boot (java.lang.SecurityException: Permission Denial)Android - 尝试在启动时测试服务 (java.lang.SecurityException: Permission Denial)
【发布时间】:2017-04-03 13:35:03
【问题描述】:

当设备在 android 上启动时,我一直在尝试测试服务,但我无法让它工作。 我正在尝试使用 CMD 中的此命令启动它:

(在 ..\AppData\Local\Android\sdk\platform-tools 中)

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n net.fstab.checkit_android/.MyReceiver

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tabache.sciopero">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:name="com.example.tabache.sciopero.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!-- Declaring broadcast receiver for BOOT_COMPLETED event.  PER FARE UN SERVIZIO AVVIATO ALL'INIZIO -->
        <receiver android:name="com.example.tabache.sciopero.MyReceiver" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <activity android:name=".MainActivity" android:exported="true">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
     App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

我的接收器类是这样的:

public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    System.out.println("MyReceiver !!!!");
    Intent startServiceIntent = new Intent(context, MioServizio.class);

context.startService(startServiceIntent);
}
}

我的 dos 命令的答案是这样的:

Broadcasting: Intent { act=android.intent.action.BOOT_COMPLETED cat=[android.intent.category.HOME] cmp=net.fstab.checkit_android/.MyReceiver }
java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.BOOT_COMPLETED from pid=3715, uid=2000
        at android.os.Parcel.readException(Parcel.java:1683)
        at android.os.Parcel.readException(Parcel.java:1636)
        at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:3507)
        at com.android.commands.am.Am.sendBroadcast(Am.java:772)
        at com.android.commands.am.Am.onRun(Am.java:404)
        at com.android.internal.os.BaseCommand.run(BaseCommand.java:51)
        at com.android.commands.am.Am.main(Am.java:121)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(NativeMethod)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:262)

为什么我有这个错误“java.lang.SecurityException: Permission Denial”?

【问题讨论】:

    标签: android broadcastreceiver adb boot android-broadcast


    【解决方案1】:

    我有同样的问题并通过这个解决:

    尝试以root模式重新启动ADB:adb root

    然后像这样广播 BOOT_COMPLETED

    adb shell am 广播 -a android.intent.action.BOOT_COMPLETED
    -p yourpackage.app

    【讨论】:

    • 是的,这样就可以了!我运行然后用这个指令广播:“adb shell am broadcast -a android.intent.action.BOOT_COMPLETED”没有“-p yourpackage.app”
    • 如果它说“adbd cannot run as root in production builds”,参考stackoverflow.com/a/45668555/1000551
    • PSA:adb root 仅适用于有根设备或模拟器没有播放服务。谢谢!
    • 我曾使用adb root,现在我不断收到Broadcast completed: result=0
    【解决方案2】:

    如果 adb root 不工作(生产版本), 在您的清单中使用:

    改为android:name="android.intent.action.ACTION_BOOT_COMPLETED

    从终端:

    adb shell am broadcast -a android.intent.action.ACTION_BOOT_COMPLETED
    

    【讨论】:

    • @AbhirojPanwar 在这个答案中,前几句话是:“如果 adb root 不起作用......”。 Root 在生产版本中根本不可用。
    • 下的 ... 声明中
    • 为我工作。 BOOT_COMPLETE 和 ACTION_BOOT_COMPLETED 有什么区别?
    • @LifeQuestioner,后者不是系统 Intent,它可能是“DonaldDuck”,它是一样的。所以,一定要修改它只是为了测试。
    • @Ron____ 答案不起作用。 ACTION_BOOT_COMPLETED 不是系统意图。当您从命令行发送此意图的广播时,您将收到消息“广播:意图 { act=android.intent.action.ACTION_BOOT_COMPLETED } 广播已完成:结果 = 0”但是 OP 想要广播系统意图 BOOT_COMPLETED。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 2011-02-16
    相关资源
    最近更新 更多