如果没有活动intent-filter,则不会收到android BOOT_COMPLETED

时间:2013-07-31 15:49:22

标签: android broadcastreceiver android-manifest hidden

我设置了一个简单的应用程序。我不想把它从抽屉里隐藏起来,我想添加一个Boot Receiver来启动服务。

为了隐藏应用程序,我读到我必须从清单中删除它

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

但是当我删除它时,启动接收器不再工作了。

我在清单标记

下添加了权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

我的接收器正在申请中

<receiver android:name="com.example.receiver.BootReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

在接收器代码中,只有一个Toast

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Boot Received", Toast.LENGTH_SHORT).show();
    }
}

为什么我不能从抽屉中设置启动接收器和隐藏的应用程序?

由于

1 个答案:

答案 0 :(得分:5)

Android 3.1 开始,所有应用程序在安装时都处于“已停止”状态。(这与应用程序结束后的状态相同用户强制 - 从“设置”应用程序中停止应用程序。)

Android stopped state

当处于“已停止”状态时,应用程序将无法以任何理由运行,除非手动启动活动。 (含义BroadcastReceviersACTION_PACKAGE_INSTALLEDBOOT_COMPLETED等)将被调用,无论他们注册的事件是什么,直到用户手动运行应用程序。)

这是Google的反恶意软件举措。谷歌一直主张用户应该首先从启动器启动一个活动,然后该应用程序可以做很多事情。在活动发起之前阻止BOOT_COMPLETED被传递是该论点的逻辑结果。

关于此的更多细节:
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
http://commonsware.com/blog/2011/07/05/boot-completed-regression.html
http://devmaze.wordpress.com/2011/12/05/activating-applications/