我一直在尝试创建一个没有 Activity 的应用服务,但遇到了一些问题。
我希望服务从开机运行,所以很自然地使用了一个BroadcastReciever
来捕获ACTION_BOOT_COMPLETED
,这在测试时没有问题。我使用 Activity 来启动和停止服务以测试它是否正常工作,然后重新启动以查看引导接收器是否正常工作,确实如此,快乐的日子。
从应用程序中删除了测试 Activity 并使用了以下 list 。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- <activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> -->
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".SWKeepAliveService" />
</application>
</manifest>
卸载然后重新安装应用程序,重新启动设备,但没有任何反应。用 adb shell am broadcast -a android.intent.action.BOOT_COMPLETED
测试了几次,仍然没有。
经过反复试验,我发现如果应用程序从未运行过,广播接收器永远不会收到 ACTION_BOOT_COMPLETED
。当然,如果没有启动器,它永远不会运行。
我错过了什么吗?我在文档中没有看到任何提及。
编辑 1
用我的 AOSP 构建进行了一些测试,上面的 list 在 Gingerbread 上没问题,但在 Jelly Bean 上不行。一定有什么改变了,我只能假设这是出于安全原因(可以理解)。虽然我还没有看到任何文件来支持这一事实。
最佳答案
Have i missed something?
是的。
I haven't seen any mention of this in the documentation.
它在 the Android 3.1 release documentation 中提到过, blog posts by balding guys , 和一个看似 infinite数量answers在 StackOverflow 上。
关于android - 如果应用程序尚未运行,广播接收器不会收到 ACTION_BOOT_COMPLETED?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12408911/