InputMethodManagerService处理输入法——监听APK变动
android\frameworks\base\services\java\com\android\server\InputMethodManagerService.java public InputMethodManagerService(Context context, WindowManagerService windowManager) {
....... //构建IME通知栏显示信息
mImeSwitcherNotification = new Notification();
mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
mImeSwitcherNotification.when = ;
mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
mImeSwitcherNotification.tickerText = null;
mImeSwitcherNotification.defaults = ; // please be quiet
mImeSwitcherNotification.sound = null;
mImeSwitcherNotification.vibrate = null; // Tag this notification specially so SystemUI knows it's important
mImeSwitcherNotification.kind = new String[] { "android.system.imeswitcher" };
//此Intent弹出选择输入法的Dialog
Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, , intent, );
...... //监听灭屏,亮屏
final IntentFilter broadcastFilter = new IntentFilter();
broadcastFilter.addAction(Intent.ACTION_SCREEN_ON);
broadcastFilter.addAction(Intent.ACTION_SCREEN_OFF);
broadcastFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
mContext.registerReceiver(new ImmsBroadcastReceiver(), broadcastFilter);
...... //监听APK安装,卸载,不可用/可用,等变动
mMyPackageMonitor.register(mContext, null, UserHandle.ALL, true);
...... //获取系统中输入法列表
mSettings = new InputMethodSettings(
mRes, context.getContentResolver(), mMethodMap, mMethodList, userId);
......
final String defaultImiId = mSettings.getSelectedInputMethod();
if (DEBUG) {
Slog.d(TAG, "Initial default ime = " + defaultImiId);
}
mImeSelectedOnBoot = !TextUtils.isEmpty(defaultImiId); buildInputMethodListLocked(mMethodList, mMethodMap,
!mImeSelectedOnBoot /* resetDefaultEnabledIme */);
mSettings.enableAllIMEsIfThereIsNoEnabledIME(); //监听Settings数据库
mSettingsObserver = new SettingsObserver(mHandler); ......
//监听系统语言变化
final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_LOCALE_CHANGED);
mContext.registerReceiver(
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
synchronized(mMethodMap) {
resetStateIfCurrentLocaleChangedLocked();
}
}
},
filter);
}
主要看一下对APK安装/卸载,不可用/可用,等变动的监听。
private final MyPackageMonitor mMyPackageMonitor = new MyPackageMonitor(); class MyPackageMonitor extends PackageMonitor { ...... @Override
public void onSomePackagesChanged() { ......
//判断输入法APK安装,卸载,然后更新IME
int change = isPackageDisappearing(imi.getPackageName());
if (isPackageModified(imi.getPackageName())) {
mFileManager.deleteAllInputMethodSubtypes(imiId);
}
if (change == PACKAGE_TEMPORARY_CHANGE
|| change == PACKAGE_PERMANENT_CHANGE) {
Slog.i(TAG, "Input method uninstalled, disabling: "
+ imi.getComponent());
setInputMethodEnabledLocked(imi.getId(), false); }
......
}
}
PackageMonitor是系统源码中监听APP变动的现成代码。可以拿来直接用,贴上完整源码。
android\frameworks\base\core\java\com\android\internal\content\PackageMonitor.java package com.android.internal.content; import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.UserHandle;
import com.android.internal.os.BackgroundThread; import java.util.HashSet; /**
* Helper class for monitoring the state of packages: adding, removing,
* updating, and disappearing and reappearing on the SD card.
*/
public abstract class PackageMonitor extends android.content.BroadcastReceiver {
static final IntentFilter sPackageFilt = new IntentFilter();
static final IntentFilter sNonDataFilt = new IntentFilter();
static final IntentFilter sExternalFilt = new IntentFilter(); static {
sPackageFilt.addAction(Intent.ACTION_PACKAGE_ADDED);
sPackageFilt.addAction(Intent.ACTION_PACKAGE_REMOVED);
sPackageFilt.addAction(Intent.ACTION_PACKAGE_CHANGED);
sPackageFilt.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
sPackageFilt.addAction(Intent.ACTION_PACKAGE_RESTARTED);
sPackageFilt.addAction(Intent.ACTION_UID_REMOVED);
sPackageFilt.addDataScheme("package");
sNonDataFilt.addAction(Intent.ACTION_UID_REMOVED);
sNonDataFilt.addAction(Intent.ACTION_USER_STOPPED);
sExternalFilt.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
sExternalFilt.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
} final HashSet<String> mUpdatingPackages = new HashSet<String>(); Context mRegisteredContext;
Handler mRegisteredHandler;
String[] mDisappearingPackages;
String[] mAppearingPackages;
String[] mModifiedPackages;
int mChangeType;
int mChangeUserId = UserHandle.USER_NULL;
boolean mSomePackagesChanged; String[] mTempArray = new String[1]; public void register(Context context, Looper thread, boolean externalStorage) {
register(context, thread, null, externalStorage);
} public void register(Context context, Looper thread, UserHandle user,
boolean externalStorage) {
if (mRegisteredContext != null) {
throw new IllegalStateException("Already registered");
}
mRegisteredContext = context;
if (thread == null) {
mRegisteredHandler = BackgroundThread.getHandler();
} else {
mRegisteredHandler = new Handler(thread);
}
if (user != null) {
context.registerReceiverAsUser(this, user, sPackageFilt, null, mRegisteredHandler);
context.registerReceiverAsUser(this, user, sNonDataFilt, null, mRegisteredHandler);
if (externalStorage) {
context.registerReceiverAsUser(this, user, sExternalFilt, null,
mRegisteredHandler);
}
} else {
context.registerReceiver(this, sPackageFilt, null, mRegisteredHandler);
context.registerReceiver(this, sNonDataFilt, null, mRegisteredHandler);
if (externalStorage) {
context.registerReceiver(this, sExternalFilt, null, mRegisteredHandler);
}
}
} public Handler getRegisteredHandler() {
return mRegisteredHandler;
} public void unregister() {
if (mRegisteredContext == null) {
throw new IllegalStateException("Not registered");
}
mRegisteredContext.unregisterReceiver(this);
mRegisteredContext = null;
} //not yet implemented
boolean isPackageUpdating(String packageName) {
synchronized (mUpdatingPackages) {
return mUpdatingPackages.contains(packageName);
}
} public void onBeginPackageChanges() {
} /**
* Called when a package is really added (and not replaced).
*/
public void onPackageAdded(String packageName, int uid) {
} /**
* Called when a package is really removed (and not replaced).
*/
public void onPackageRemoved(String packageName, int uid) {
} /**
* Called when a package is really removed (and not replaced) for
* all users on the device.
*/
public void onPackageRemovedAllUsers(String packageName, int uid) {
} public void onPackageUpdateStarted(String packageName, int uid) {
} public void onPackageUpdateFinished(String packageName, int uid) {
} /**
* Direct reflection of {@link Intent#ACTION_PACKAGE_CHANGED
* Intent.ACTION_PACKAGE_CHANGED} being received, informing you of
* changes to the enabled/disabled state of components in a package
* and/or of the overall package.
*
* @param packageName The name of the package that is changing.
* @param uid The user ID the package runs under.
* @param components Any components in the package that are changing. If
* the overall package is changing, this will contain an entry of the
* package name itself.
* @return Return true to indicate you care about this change, which will
* result in {@link #onSomePackagesChanged()} being called later. If you
* return false, no further callbacks will happen about this change. The
* default implementation returns true if this is a change to the entire
* package.
*/
public boolean onPackageChanged(String packageName, int uid, String[] components) {
if (components != null) {
for (String name : components) {
if (packageName.equals(name)) {
return true;
}
}
}
return false;
} public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
return false;
} public void onHandleUserStop(Intent intent, int userHandle) {
} public void onUidRemoved(int uid) {
} public void onPackagesAvailable(String[] packages) {
} public void onPackagesUnavailable(String[] packages) {
} public static final int PACKAGE_UNCHANGED = 0;
public static final int PACKAGE_UPDATING = 1;
public static final int PACKAGE_TEMPORARY_CHANGE = 2;
public static final int PACKAGE_PERMANENT_CHANGE = 3; /**
* Called when a package disappears for any reason.
*/
public void onPackageDisappeared(String packageName, int reason) {
} /**
* Called when a package appears for any reason.
*/
public void onPackageAppeared(String packageName, int reason) {
} /**
* Called when an existing package is updated or its disabled state changes.
*/
public void onPackageModified(String packageName) {
} public boolean didSomePackagesChange() {
return mSomePackagesChanged;
} public int isPackageAppearing(String packageName) {
if (mAppearingPackages != null) {
for (int i=mAppearingPackages.length-1; i>=0; i--) {
if (packageName.equals(mAppearingPackages[i])) {
return mChangeType;
}
}
}
return PACKAGE_UNCHANGED;
} public boolean anyPackagesAppearing() {
return mAppearingPackages != null;
} public int isPackageDisappearing(String packageName) {
if (mDisappearingPackages != null) {
for (int i=mDisappearingPackages.length-1; i>=0; i--) {
if (packageName.equals(mDisappearingPackages[i])) {
return mChangeType;
}
}
}
return PACKAGE_UNCHANGED;
} public boolean anyPackagesDisappearing() {
return mDisappearingPackages != null;
} public boolean isPackageModified(String packageName) {
if (mModifiedPackages != null) {
for (int i=mModifiedPackages.length-1; i>=0; i--) {
if (packageName.equals(mModifiedPackages[i])) {
return true;
}
}
}
return false;
} public void onSomePackagesChanged() {
} public void onFinishPackageChanges() {
} public int getChangingUserId() {
return mChangeUserId;
} String getPackageName(Intent intent) {
Uri uri = intent.getData();
String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
return pkg;
} @Override
public void onReceive(Context context, Intent intent) {
mChangeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
UserHandle.USER_NULL);
if (mChangeUserId == UserHandle.USER_NULL) {
throw new IllegalArgumentException(
"Intent broadcast does not contain user handle: " + intent);
}
onBeginPackageChanges(); mDisappearingPackages = mAppearingPackages = null;
mSomePackagesChanged = false; String action = intent.getAction();
if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
String pkg = getPackageName(intent);
int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
// We consider something to have changed regardless of whether
// this is just an update, because the update is now finished
// and the contents of the package may have changed.
mSomePackagesChanged = true;
if (pkg != null) {
mAppearingPackages = mTempArray;
mTempArray[0] = pkg;
if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
mModifiedPackages = mTempArray;
mChangeType = PACKAGE_UPDATING;
onPackageUpdateFinished(pkg, uid);
onPackageModified(pkg);
} else {
mChangeType = PACKAGE_PERMANENT_CHANGE;
onPackageAdded(pkg, uid);
}
onPackageAppeared(pkg, mChangeType);
if (mChangeType == PACKAGE_UPDATING) {
synchronized (mUpdatingPackages) {
mUpdatingPackages.remove(pkg);
}
}
}
} else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
String pkg = getPackageName(intent);
int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
if (pkg != null) {
mDisappearingPackages = mTempArray;
mTempArray[0] = pkg;
if (intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
mChangeType = PACKAGE_UPDATING;
synchronized (mUpdatingPackages) {
//not used for now
//mUpdatingPackages.add(pkg);
}
onPackageUpdateStarted(pkg, uid);
} else {
mChangeType = PACKAGE_PERMANENT_CHANGE;
// We only consider something to have changed if this is
// not a replace; for a replace, we just need to consider
// it when it is re-added.
mSomePackagesChanged = true;
onPackageRemoved(pkg, uid);
if (intent.getBooleanExtra(Intent.EXTRA_REMOVED_FOR_ALL_USERS, false)) {
onPackageRemovedAllUsers(pkg, uid);
}
}
onPackageDisappeared(pkg, mChangeType);
}
} else if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
String pkg = getPackageName(intent);
int uid = intent.getIntExtra(Intent.EXTRA_UID, 0);
String[] components = intent.getStringArrayExtra(
Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST);
if (pkg != null) {
mModifiedPackages = mTempArray;
mTempArray[0] = pkg;
mChangeType = PACKAGE_PERMANENT_CHANGE;
if (onPackageChanged(pkg, uid, components)) {
mSomePackagesChanged = true;
}
onPackageModified(pkg);
}
} else if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) {
mDisappearingPackages = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
mChangeType = PACKAGE_TEMPORARY_CHANGE;
boolean canRestart = onHandleForceStop(intent,
mDisappearingPackages,
intent.getIntExtra(Intent.EXTRA_UID, 0), false);
if (canRestart) setResultCode(Activity.RESULT_OK);
} else if (Intent.ACTION_PACKAGE_RESTARTED.equals(action)) {
mDisappearingPackages = new String[] {getPackageName(intent)};
mChangeType = PACKAGE_TEMPORARY_CHANGE;
onHandleForceStop(intent, mDisappearingPackages,
intent.getIntExtra(Intent.EXTRA_UID, 0), true);
} else if (Intent.ACTION_UID_REMOVED.equals(action)) {
onUidRemoved(intent.getIntExtra(Intent.EXTRA_UID, 0));
} else if (Intent.ACTION_USER_STOPPED.equals(action)) {
if (intent.hasExtra(Intent.EXTRA_USER_HANDLE)) {
onHandleUserStop(intent, intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
}
} else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) {
String[] pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
mAppearingPackages = pkgList;
mChangeType = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)
? PACKAGE_UPDATING : PACKAGE_TEMPORARY_CHANGE;
mSomePackagesChanged = true;
if (pkgList != null) {
onPackagesAvailable(pkgList);
for (int i=0; i<pkgList.length; i++) {
onPackageAppeared(pkgList[i], mChangeType);
}
}
} else if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) {
String[] pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
mDisappearingPackages = pkgList;
mChangeType = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)
? PACKAGE_UPDATING : PACKAGE_TEMPORARY_CHANGE;
mSomePackagesChanged = true;
if (pkgList != null) {
onPackagesUnavailable(pkgList);
for (int i=0; i<pkgList.length; i++) {
onPackageDisappeared(pkgList[i], mChangeType);
}
}
} if (mSomePackagesChanged) {
onSomePackagesChanged();
} onFinishPackageChanges();
mChangeUserId = UserHandle.USER_NULL;
}
}
在实现监听APK安装,卸载时,可能用不到PackageMonitor中那么详细的处理。
但是要注意
final IntentFilter sPackageFilt = new IntentFilter();
sPackageFilt.addAction(Intent.ACTION_PACKAGE_ADDED);
sPackageFilt.addAction(Intent.ACTION_PACKAGE_REMOVED);
sPackageFilt.addAction(Intent.ACTION_PACKAGE_CHANGED);
sPackageFilt.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
sPackageFilt.addAction(Intent.ACTION_PACKAGE_RESTARTED);
sPackageFilt.addAction(Intent.ACTION_UID_REMOVED);
sPackageFilt.addDataScheme("package");//这个是必须要有的,否则监听不到。
如果在AndroidManifest.xml中配置广播监听APK变动
<receiver android:name="MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action......." />
<action android:name="android.intent.action......." />
<data android:scheme="package" /> <!--注意这行一定要加-->
</intent-filter>
</receiver>
InputMethodManagerService处理输入法——监听APK变动的更多相关文章
- 【Android】Android 监听apk安装替换卸载广播
[Android]Android 监听apk安装替换卸载广播 首先是要获取应用的安装状态,通过广播的形式 以下是和应用程序相关的Broadcast Action ACTION_PACKAGE_ADDE ...
- Android输入法 监听事件
登录界面有一个输入用户名和密码的编辑框: private EditText et_userName;// 账户 private EditText et_password;// 密码 布局文件如下: & ...
- Freemarker页面静态化技术,activemq监听页面变动
初步理解: 架构优化: 静态页面的访问速度优于从缓存获取数据的动态页面的访问速度: Freemarker: 导包 模板:hello.ftl <!DOCTYPE html> <html ...
- Android为TV端助力 监听APK卸载,替换,完成的广播
ACTION_PACKAGE_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程序不能接收到这个广播)ACTION_PACKAGE_REPLACED 一个新版本的应用安装到设备,替 ...
- Android 监听apk安装替换卸载广播
首先是要获取应用的安装状态,通过广播的形式 以下是和应用程序相关的Broadcast Action ACTION_PACKAGE_ADDED 一个新应用包已经安装在设备上,数据包括包名(最新安装的包程 ...
- Django runserver 默认多线程 监听文件变动
django-admin and manage.py | Django documentation | Django https://docs.djangoproject.com/en/3.0/ref ...
- Android 如何监听输入法关闭事件
假设有如下界面(输入法的上面的输入区域是用Dialog实现的) 要求当输入法关闭的时候,Dialog也一起关闭,这样用户就不需要返回两次了. 网上找了很多资料都没有很好的解决这个问题,输入法是第三方程 ...
- 实时监听input标签输入 实时监听文本框输入 避免中文输入法无法触发onkeyup事件的问题
前言: 对于实时监听输入,这种需求大多数都是用于一个联想字提醒,智能提醒.大家都知道onkeydown,onkeypress,onkeyup的在监听中文输入法或者右键粘贴的时候都存在一些弊端,不是那么 ...
- localStorage单页面及不同页面监听变动
分析 H5本地存储有两个API,一个是Web Storage,还有一个是Web SQL.不管是哪一个,都是基于JavaScript语言来使用,而Web Storage提供了两种存储类型 API: s ...
随机推荐
- cxf怎样提高webservice性能,及访问速度调优
性能: 1. 启用FastInfoset(快速信息集)webservice的性能实在是不敢恭维.曾经因为webservice吞吐量上不去,对webservice进行了一些性能方面的优化,采用了Fast ...
- 如何搭建python+selenium2+eclipse的环境
搭建python和selenium2的环境(windows) 1.下载并安装python(我用的是2.7的版本) 可以去python官网下载安装:http://www.python.org/getit ...
- centos搭建OPENldap
LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP.它是基于X.500标准的,但是简单多了并且可以根据需要定制.与X ...
- leetcode 数组类型题总结
1,removeDuplicates(I) int removeDuplicatesI(vector<int>& nums){ // 重新组织数组,同 removeDuplicat ...
- 八:python 对象类型详解四:字典
一:字典通识: 1,字典通过键而不是偏移量来读取: 2,字典是任意对象的无序集合: 3,字典可变长.异构.任意嵌套: 4,字典属于可变映射类型: 5,对象引用表(散列表): 二:实际应用中的字典: 1 ...
- 电商项目中学到的git命令
1.在拉下来的文件夹被删除后的操作 创建了文件后 git init 增加了 .git文件 ls -al 查看后有.git文件夹 git remote add origin (ssh) 连接到git仓库 ...
- OSPF网络类型不一致路由无法计算的问题
晚上割接,远端的ASR9001-s网络类型为广播类型,本端为6509-e,网络接口类型修改成p2p后,OSPF邻居关系建立,但是路由无法计算.
- MySQL之开启远程连接
MySQL安装时,默认只能本地连接. mysql -u root -p mysql>use mysql; mysql>select 'host' from user where user= ...
- js正则表达式中的正向肯定预查和正向否定预查
对于没有使用过这几个表达式的人,应该对这个概念都有点不太理解,下面就以实际例子说明这几个表达式的用户. 一.?:pattern——匹配检验:会作为匹配校验,是一个非获取匹配,并出现在匹配字符结果里面, ...
- css控制div上浮下落
CSS3 示例:http://www.w3school.com.cn/cssref/pr_keyframes.asp 以下是代码: <!DOCTYPE html> <html> ...