未启动的应用无法监听到系统广播

先说下问题和解决方案:

Q:在 3.1 版本以后,新安装而从未启动过的app不能收到系统的广播(启动完成,网络状态变化之类的);

解决方案:
1. 将app做成系统应用,直接安装在 system/app 目录下
2. 通过第三方应用,发送带 FLAG_INCLUDE_STOPPED_PACKAGES 的广播给stop状态的自己


下文转载自 http://www.cnblogs.com/fanfeng/p/3236178.html ,很好的讲解了这个问题的原因

Android 开机自启动

首先实现开机自启动:

第一步创建一个广播接收者,如MyBootBroadcastReceiver.java

package com.example;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBootBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, MyService.class);
        context.startService(startServiceIntent);
        //启动应用
        //Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
        //context.startActivity(intent);
    }
}

第二步给receiver配置对应intent-filter

<receiver android:name="MyBootBroadcastReceiver">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>

第三步添加权限,缺少这步则无法在Android 3.0及其之前版本上自启动

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

上面的实现有以下【限制】:

1.由于Android系统在外置SD卡加载前广播ACTION_BOOT_COMPLETED,于是如果应用安装在外置SD卡上,它无法接收到此广播,原文见Broadcast Receivers listening for "boot completed"

2.从Android 3.1(API level对应NDK版本)开始,系统的包管理器保持跟踪处于停止状态(stopped state)的应用程序,提供了一种控制其从后台进程和其它应用程序启动的方式。这种停止状态的应用程序指那些安装了但从未启动过的apk,或被用户在程序管理中force stop的apk。Android系统为防止广播无意或不必要开启停止状态的组件,它给所有广播intent添加了FL​​AG_EXCLUDE_STOPPED_PACKAGES标志(不设置和同FLAG_INCLUDE_STOPPED_PACKAGES一起设置结果都是此标志),

Intent.java

public boolean isExcludingStopped() {
        return (mFlags&(FLAG_EXCLUDE_STOPPED_PACKAGES|FLAG_INCLUDE_STOPPED_PACKAGES))
                == FLAG_EXCLUDE_STOPPED_PACKAGES;
    }

IntentResolver.java

/**
     * Returns whether the object associated with the given filter is
     * "stopped," that is whether it should not be included in the result
     * if the intent requests to excluded stopped objects.
     */
    protected boolean isFilterStopped(F filter, int userId) {
        return false;
    }

private void buildResolveList(Intent intent, FastImmutableArraySet<String> categories,
            boolean debug, boolean defaultOnly,
            String resolvedType, String scheme, F[] src, List<R> dest, int userId) {
        ...

        final boolean excludingStopped = intent.isExcludingStopped();

        final int N = src != null ? src.length : 0;
        boolean hasNonDefaults = false;
        int i;
        F filter;
        for (i=0; i<N && (filter=src[i]) != null; i++) {
            int match;
            if (debug) Slog.v(TAG, "Matching against filter " + filter);

            if (excludingStopped && isFilterStopped(filter, userId)) {
                if (debug) {
                    Slog.v(TAG, "  Filter's target is stopped; skipping");
                }
                continue;
            }
        ...

此标志指广播intent排除停止状态的应用,原文见Launch controls on stopped applications。用户可以给自己的应用或者后台服务添加FLAG_INCLUDE_STOPPED_PACKAGES标志以唤醒停止状态的应用,但系统自带的广播intent,用户无法修改,只能接受;注意系统级应用都不是停止状态

PackageManagerService.java中重写IntentResolver

final class ActivityIntentResolver
            extends IntentResolver<PackageParser.ActivityIntentInfo, ResolveInfo> {
        ...
@Override
        protected boolean isFilterStopped(PackageParser.ActivityIntentInfo filter, int userId) {
            if (!sUserManager.exists(userId)) return true;
            PackageParser.Package p = filter.activity.owner;
            if (p != null) {
                PackageSetting ps = (PackageSetting)p.mExtras;
                if (ps != null) {
                    // System apps are never considered stopped for purposes of
                    // filtering, because there may be no way for the user to
                    // actually re-launch them.
                    return (ps.pkgFlags&ApplicationInfo.FLAG_SYSTEM) == 0
                            && ps.getStopped(userId);
                }
            }
            return false;
        }

private final class ServiceIntentResolver
            extends IntentResolver<PackageParser.ServiceIntentInfo, ResolveInfo> {
        ...
@Override
        protected boolean isFilterStopped(PackageParser.ServiceIntentInfo filter, int userId) {
            if (!sUserManager.exists(userId)) return true;
            PackageParser.Package p = filter.service.owner;
            if (p != null) {
                PackageSetting ps = (PackageSetting)p.mExtras;
                if (ps != null) {
                    // System apps are never considered stopped for purposes of
                    // filtering, because there may be no way for the user to
                    // actually re-launch them.
                    return (ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) == 0
                            && ps.getStopped(userId);
                }
            }
            return false;
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值