Android 设备引导完成后无法启动服务。引导完成BroadcastReceive()未调用

Android 设备引导完成后无法启动服务。引导完成BroadcastReceive()未调用,android,broadcastreceiver,bootcompleted,Android,Broadcastreceiver,Bootcompleted,我正在开发一个应用程序,它将在设备启动时(启动完成后)自动启动服务。我确实编写了代码,但我认为我的代码出了问题。我也尝试过谷歌和所有的研究技巧。你能帮帮我吗?提前准备好 这是我的密码: 我的广播接收器是 public class AutostartReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (in

我正在开发一个应用程序,它将在设备启动时(启动完成后)自动启动服务。我确实编写了代码,但我认为我的代码出了问题。我也尝试过谷歌和所有的研究技巧。你能帮帮我吗?提前准备好

这是我的密码:

我的广播接收器是

public class AutostartReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Toast.makeText(this, "Booting Completed", Toast.LENGTH_LONG).show(); 
            //context.startService(new Intent(context, MyService.class));
        }
    }
}
我的AndroidMenifest文件是

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myhiddenapp"
    android:installLocation="internalOnly"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="MyHiddenApp" >
        <receiver
            android:name=".AutostartReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service android:name=".MyService" />
    </application>

</manifest>

注意:我没有任何活动,我只想在启动完成后启动服务。


您必须设置android:installLocation=“internalOnly”


如果该应用程序在Android版本>3.0上运行,则需要至少启动一次的活动,才能使应用程序脱离停止状态。否则,您的接收器将无法工作。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:installLocation="internalOnly"
    ... >