1

老实说,他们什么也没做。首先让我说我知道 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 仍然没有运气。

4

1 回答 1

0

我找到了答案,我认为这实际上很重要,因为这似乎是其他人在某些时候会遇到的问题。我的问题是我正在启动一个名为 LauncherActivity 的“启动器活动”。基本上,它充当了我在启动应用程序时启动服务、接收器等的网关。这是一个非常简单的 Activity 类:

public class LaunchActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    /* 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.speedapp.intent");
    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    this.sendBroadcast(intent);
}

@Override
protected void onStart(){
    super.onStart();

    Intent serviceIntent = new Intent(this, MainService.class);
    startService(serviceIntent);

    // NOTE: It is VERY, VERY important that we DO NOT finish this activity.
    // If we do, our BootReceiver will no longer receive its broadcast and 
    // we won't auto-start on boot
    //finish();
}

}

请注意,我在 LaunchActivity 上调用了 finish()。问题在于它似乎告诉 Android 应用程序处于停止状态,这意味着它不允许接收 BOOT_COMPLETED 广播,即使我在后台运行了许多服务。LauncherActivity 必须在整个生命周期中保持活动状态,否则 BOOT_COMPLETED 接收器将变得无用。我以前没有真正看到过的东西,但我认为还是值得注意的东西。

于 2014-03-13T13:32:08.790 回答