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」が発生するのはなぜですか?
私は同じ問題を抱えていて、これで解決しました:
ADBをrootモードで再起動してみてください:adb root
次に、このようにBOOT_COMPLETEDをブロードキャストします
adb Shell am broadcast -a Android.intent.action.BOOT_COMPLETED
-p yourpackage.app
Adb rootが機能しない場合(本番ビルド)、マニフェストで使用します。
Android:name="Android.intent.action.ACTION_BOOT_COMPLETED
代わりに。
そしてターミナルから:
adb Shell am broadcast -a Android.intent.action.ACTION_BOOT_COMPLETED