2016-11-19 91 views
3

我一直试图在Android上启动设备时尝试测试服务,但我无法使其运行。 我试图从CMD命令启动它:Android - 试图在启动时测试服务(java.lang.SecurityException:权限拒绝)

(在.. \应用程序数据\本地\ Android的\ SDK \平台工具)

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> 

我的接收机CL驴是这样的:

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异常:权限拒绝”?

回答

17

我有同样的问题,通过这个

 
Try restarting ADB in root mode: adb root 

解决再播BOOT_COMPLETED这样

 
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED 
-p yourpackage.app
+0

是的,这样它的作品!我运行然后广播与此istru:“adb外壳广播-a android.intent.action.BOOT_COMPLETED”没有“-p yourpackage.app” – tabache

2

如果亚行根本不工作在你的清单(生产版本), 用途:

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

,并从终端:

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

如何让adb root工作? –

+0

@AbhirojPanwar在这个答案中,前几个单词是:“如果adb root不工作......”。 Root在生产版本中根本不可用。 – Micer

相关问题