未启动的应用无法监听到系统广播
来源:互联网 发布:知天下论坛 编辑:程序博客网 时间:2024/06/03 01:45
先说下问题和解决方案:
下文转载自 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添加了FLAG_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; }
- 未启动的应用无法监听到系统广播
- 未启动的应用无法监听到系统广播
- Android应用在未启动的情况下无法收到指定广播的问题总结
- Android应用在未启动的情况下无法收到指定广播的问题总结
- Android应用在未启动的情况下无法收到指定广播的问题总结
- Android应用在未启动的情况下无法收到指定广播的问题总结
- Android应用在安装后未启动的情况下无法收到开机等各类广播
- 监听系统开机广播并启动Activity
- 监听应用的启动
- windows 系统 oracle监听无法启动。
- 常用的监听系统广播接收器
- 监听程序未启动或数据库服务未注册到该监听程序解决方法
- 监听程序未启动或数据库服务未注册到该监听程序
- ORACLE“监听程序未启动或数据库服务未注册到该监听程序”解决方法
- ORACLE“监听程序未启动或数据库服务未注册到该监听程序”解决方法
- 严重: 监听程序未启动或数据库服务未注册到该监听程序。解决方法
- Android监听系统广播 和自定义广播遇到的问题
- 监听系统广播Action
- qt5把qt_zh_CN.qm加载了,但QTextEdit的右键菜单仍然是英文,而QLineEdit的倒是中文。怎样设置使得QTextEdit的右键菜单是中文
- 欢迎使用CSDN-markdown编辑器
- linux文件函数-read
- Task Affinity
- 内核模块的加载
- 未启动的应用无法监听到系统广播
- protobuf的简单应用,json和protocol Buffer的转换简单例子
- LinkedList底层实现
- CAS SSO 4.0 用户数据库验证
- hihoCoder 1081 : 最短路径·一
- 【MAC使用技巧】Safari、qq浏览器等设置F5刷新快捷键
- python中的sort方法使用详解
- linux下memcached的安装启动/结束的方式
- PullToRefreshListView 自动刷新