Android-尝试在启动时测试服务(java.lang.SecurityException:权限被拒绝) [英] Android - Trying to test a service on boot (java.lang.SecurityException: Permission Denial)
本文介绍了Android-尝试在启动时测试服务(java.lang.SecurityException:权限被拒绝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试在Android设备上启动设备时测试一项服务,但无法使其正常工作。
我正在尝试使用CMD中的以下命令启动它:
I've been trying to test a service when a device boots up on android, but I cannot get it to work. I'm trying to start it with this command from CMD:
(在..\AppData\Local\Android\sdk\平台工具)
(in ..\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命令的答案是:
the answer to my dos command is this:
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:权限被拒绝?
Why I have this error "java.lang.SecurityException: Permission Denial"?
推荐答案
我遇到了同样的问题,并通过以下方法解决了:
I had same problem and solve by this:
Try restarting ADB in root mode: adb root
然后像这样广播BOOT_COMPLETED
and then broadcast BOOT_COMPLETED like this
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED
-p yourpackage.app
这篇关于Android-尝试在启动时测试服务(java.lang.SecurityException:权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文