老实说,他们什么也没做。首先让我说我知道 Android 在 3.1 中重新设计了接收器,特别是启动控制。我知道他们这样做是为了使 ACTION_BOOT_COMPLETED 不能使用,除非该应用程序先前已由用户启动。然而,人们已经成功地在当前的应用程序中使用它们,但我从来没有因为我的 BOOT_COMPLETED 或我的 SHUTDOWN 而击中我的接收器。
快速编辑 - 请查看这篇文章的底部以获取更正的 Shutdown Receiver,我已经让它工作了,现在我正努力让 BOOT_COMPLETED 工作。
我的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smashingboxes.speedblock"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<!-- PERMISSIONS -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<!-- RECEIVERS -->
<receiver android:name=".BootReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".ShutdownReceiver" >
<intent-filter>
<action android:name="android.intent.action.SHUTDOWN" />
</intent-filter>
</receiver>
现在我实现的接收器类相当简单:
BOOT_COMPLETED 接收器(不工作的那个)
public class BootReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context c, Intent i) {
Intent starterIntent = new Intent(c, LaunchActivity.class);
// Start the activity (by utilizing the passed context)
starterIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.getApplicationContext().startService(starterIntent);
}
}
根据我所看到的解决方案,我尝试了不同的事情,例如改变我的启动活动以包括
/* May need this, as of 3.1 we can't call BOOT_COMPLETED until the app has been run successfully */
Intent intent = new Intent("com.smashingboxes.speedblock.intent");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
this.sendBroadcast(intent);
或将其包含在清单中的引导接收器意图过滤器中
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
似乎没有任何效果。当日志插入我的接收器方法时,它们永远不会被命中。显然人们仍在相当频繁地使用这两个接收器,这就是为什么我无法理解为什么它们都不起作用的原因。我的注册是否遗漏了什么?
- 编辑 -
我已经解决了我的关机接收器的问题。首先,我愚蠢地忘记了标签的 ACTION_ 部分。其次,HTC 有单独的关闭方法,在我的情况下,我需要在我的 Receiver 请求中添加一个意图过滤器:
<receiver android:name=".ShutdownReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
</intent-filter>
</receiver>
现在我的 Shutdown Receiver 工作了,但 Boot Completed Receiver 仍然没有运气。