一个应用没有activity,在API<3.1的版本是可以的接受广播,3.1以后就不可以了。

从3.1开始出于安全的考虑,一个应用必须至少运行一次才能接受广播,意思是说用户知道这个应用运行过,它才可以接受广播。如果非要试试没有activity的话,可以第一次安装有activity,然后,删除activity和xml对应的配置信息再次安装。不过这样实际意义不大。

普通的App如果没有启动默认都是停止状态的,停止状态的App默认是接受不到任何广播的。不过发送广播时如果添加指定标记,也可以使停止状态的应用接受这个的广播。

/**
 * If set, this intent will not match any components in packages that
 * are currently stopped.  If this is not set, then the default behavior
 * is to include such applications in the result.
 */
public static final int FLAG_EXCLUDE_STOPPED_PACKAGES = 0x00000010;
/**
 * If set, this intent will always match any components in packages that
 * are currently stopped.  This is the default behavior when
 * {@link# FLAG_EXCLUDE_STOPPED_PACKAGES} is not set.  If both of these
 * flags are set, this one wins (it allows overriding of exclude for
 * places where the framework may automatically set the exclude flag).
 */
public static final int FLAG_INCLUDE_STOPPED_PACKAGES = 0x00000020;

以上是相关的intent的flag设置。注释已经说的很清楚了,FLAG_INCLUDE_STOPPED_PACKAGES是默认行为,如果2个标记都设置的话,也是这个标记主导:停止状态的应用也能接受发送的广播。但是上面已经描述过了:

手头最新的系统源码4.2的,找到了相关设置,在以下文件中:

frameworks/base/services/java/com/android/server/pm/PackageManagerService.java

/**
 * If set, this intent will not match any components in packages that
 * are currently stopped.  If this is not set, then the default behavior
 * is to include such applications in the result.
 */
public static final int FLAG_EXCLUDE_STOPPED_PACKAGES = 0x00000010;
/**
 * If set, this intent will always match any components in packages that
 * are currently stopped.  This is the default behavior when
 * {@link# FLAG_EXCLUDE_STOPPED_PACKAGES} is not set.  If both of these
 * flags are set, this one wins (it allows overriding of exclude for
 * places where the framework may automatically set the exclude flag).
 */
public static final int FLAG_INCLUDE_STOPPED_PACKAGES = 0x00000020;

广播默认不会发给停止状态的应用,除非设置过FLAG_INCLUDE_STOPPED_PACKAGES标记,但是系统(保守说大多数)广播是没有设置这个标记的,也就是说停止状态系统的应用接受不到系统广播。普通应用自定义的广播可以随便设置标记。

/**
 * If set, this intent will not match any components in packages that
 * are currently stopped.  If this is not set, then the default behavior
 * is to include such applications in the result.
 */
public static final int FLAG_EXCLUDE_STOPPED_PACKAGES = 0x00000010;
/**
 * If set, this intent will always match any components in packages that
 * are currently stopped.  This is the default behavior when
 * {@link# FLAG_EXCLUDE_STOPPED_PACKAGES} is not set.  If both of these
 * flags are set, this one wins (it allows overriding of exclude for
 * places where the framework may automatically set the exclude flag).
 */
public static final int FLAG_INCLUDE_STOPPED_PACKAGES = 0x00000020;