在Android系统中,键盘按键事件是由WindowManagerService服务来管理的,然后再以消息的形式来分发给应用程序处理,不过和普通消息不一样,它是由硬件中断触发的;本文将结合这种消息处理机制来详细分析Android应用程序是如何获得键盘按键消息的。
在系统启动的时候,SystemServer会启动窗口管理服务WindowManagerService,WindowManagerService 在启动的时候就会通过系统输入管理器InputManager来总负责监控键盘消息。这些键盘消息一般都是分发给当前激活的Activity窗口来处理的,因此,当前激活的Activity窗口在创建的时候,会到WindowManagerService中去注册一个接收键盘消息的通道,表明它要处理键盘消息,而当InputManager监控到有键盘消息时,就会分给给它处理。当当前激活的Activity窗口不再处于激活状态时,它也会到 WindowManagerService中去反注册之前的键盘消息接收通道,这样,InputManager就不会再把键盘消息分发给它来处理。
WindowManagerService中的处理函数interceptKeyBeforeDispatching和interceptKeyBeforeQueueing是在PhoneWindowManager中实现的。
Android4.x在Framework的PhoneWindowManager在监听按键事件的时候对Power(KeyEvent.KEYCODE_POWER)和Home(KeyEvent.KEYCODE_HOME)键做了处理,不会把这些键传送上层应用程序。如需要把这些键发送给Activity和Service,需要在PhoneWindowManager处理这些键时“发送一个广播出去,然后在应用程序接收到广播后做处理”。
Home键在KeyEvent中的键值为3.即:KEYCODE_HOME = 3.
当用户按下home键的时候(包括长按),程序会进入到PhoneWindowManager中的interceptKeyBeforeDispatching这个方法中进行处理。如果用户是连续点击Home,此时就要执行长按Home事件了。即执行mHandler.postDelayed(mHomeLongPress, ViewConfiguration.getGlobalActionKeyTimeout());对应的代码。也就会跳转到mHomeLongPress这个Runnable接着往下执行。
interceptKeyBeforeDispatching这个方法位于PhoneWindowManager.java中。
位置为:/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
public boolean interceptKeyBeforeDispatching(WindowState win, int action, int flags,
int keyCode, int scanCode, int metaState, int repeatCount, int policyFlags) {
final boolean down = (action == KeyEvent.ACTION_DOWN);
...
//4、用户按下home,然后马上释放。此时这个条件成立。将之前postDelayed的事件remove掉。此时就不会执行长按home事件。
if ((keyCode == KeyEvent.KEYCODE_HOME) && !down) {
mHandler.removeCallbacks(mHomeLongPress);
}
//5、第一次按下home,mHomePressed为false。
if (mHomePressed) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
//a、如果用户连续按下home,此时暂时没有up事件。所以就不走这里。
//b、如果用户没有连续按下home,此时过来的是up(move或者...)事件。即!down为true,执行该方法
if (!down) {
mHomePressed = false;
if (!canceled) {
boolean incomingRinging = false;
try {
ITelephony telephonyService = getTelephonyService();
if (telephonyService != null) {
incomingRinging = telephonyService.isRinging();
}
} catch (RemoteException ex) {
Log.w(TAG, "RemoteException from getPhoneInterface()", ex);
}
if (incomingRinging) {
Log.i(TAG, "Ignoring HOME; there's a ringing incoming call.");
} else {
//单击home处理
launchHomeFromHotKey();
}
} else {
Log.i(TAG, "Ignoring HOME; event canceled.");
}
}
}
return true;
}
...
// 1、第一次处理home按下
if (keyCode == KeyEvent.KEYCODE_HOME) {
// If a system window has focus, then it doesn't make sense
// right now to interact with applications.
WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
if (attrs != null) {
final int type = attrs.type;
if (type == WindowManager.LayoutParams.TYPE_KEYGUARD
|| type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {
// the "app" is keyguard, so give it the key
return false;
}
final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
for (int i=0; i
mHomeLongPress的代码如下。它的作用是请求生成一个弹出近期任务的对话框。执行showRecentAppsDialog()方法。
[java] view plaincopy/**
* When a home-key longpress expires, close other system windows and launch the recent apps
*/
Runnable mHomeLongPress = new Runnable() {
public void run() {
/*
* Eat the longpress so it won't dismiss the recent apps dialog when
* the user lets go of the home key
*/
mHomePressed = false;//将mHomePressed还原,就可以接收下次home单击事件了。
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_RECENT_APPS);
showRecentAppsDialog();//弹出近期任务的对话框
}
};
showRecentAppsDialog()方法如下。它会去创建一个RecentApplicationsDialog对象,从而完成弹出任务对话框的操作。
[java] view plaincopy/**
* Create (if necessary) and launch the recent apps dialog
*/
void showRecentAppsDialog() {
if (mRecentAppsDialog == null) {
mRecentAppsDialog = new RecentApplicationsDialog(mContext);
}
mRecentAppsDialog.show();
}
接下来就是创建RecentApplicationsDialog对象来显示近期任务了。
该类位置为:frameworks/base/policy/src/com/android/internal/policy/impl/RecentApplicationsDialog.java
代码中已经做了详细的注解,这里就不再仔细说。最重要的一步就是在onStart()中的reloadButtons()方法。它是获取近期任务的方法。
public class RecentApplicationsDialog extends Dialog implements OnClickListener {
// Elements for debugging support
private static final boolean DBG_FORCE_EMPTY_LIST = false;
static private StatusBarManager sStatusBar;
private static final int NUM_BUTTONS = 8;
private static final int MAX_RECENT_TASKS = NUM_BUTTONS * 2; // allow for some discards
final TextView[] mIcons = new TextView[NUM_BUTTONS];
View mNoAppsText;
IntentFilter mBroadcastIntentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);//广播接收者
Handler mHandler = new Handler();
//将每个TextView相关的intent信息和drawable信息删除
Runnable mCleanup = new Runnable() {
public void run() {
// dump extra memory we're hanging on to
for (TextView icon: mIcons) {
icon.setCompoundDrawables(null, null, null, null);
icon.setTag(null);
}
}
};
private int mIconSize;
public RecentApplicationsDialog(Context context) {
super(context, com.android.internal.R.style.Theme_Dialog_RecentApplications);
final Resources resources = context.getResources();
mIconSize = (int) resources.getDimension(android.R.dimen.app_icon_size);
}
/**
* We create the recent applications dialog just once, and it stays around (hidden)
* until activated by the user.
*
* @see PhoneWindowManager#showRecentAppsDialog
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context context = getContext();
//StatusBarManager这个系统服务只供系统内部使用,不对外开放。作用是对状态栏进行管理,我们不去管它。
//该类位置:\frameworks\base\core\java\android\app\StatusBarManager.java
if (sStatusBar == null) {
sStatusBar = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE);
}
//请求窗口的一些属性
Window window = getWindow();
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
window.setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
window.setTitle("Recents");
setContentView(com.android.internal.R.layout.recent_apps_dialog);
final WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
window.setFlags(0, WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//最多放八个近期任务
mIcons[0] = (TextView)findViewById(com.android.internal.R.id.button0);
mIcons[1] = (TextView)findViewById(com.android.internal.R.id.button1);
mIcons[2] = (TextView)findViewById(com.android.internal.R.id.button2);
mIcons[3] = (TextView)findViewById(com.android.internal.R.id.button3);
mIcons[4] = (TextView)findViewById(com.android.internal.R.id.button4);
mIcons[5] = (TextView)findViewById(com.android.internal.R.id.button5);
mIcons[6] = (TextView)findViewById(com.android.internal.R.id.button6);
mIcons[7] = (TextView)findViewById(com.android.internal.R.id.button7);
mNoAppsText = findViewById(com.android.internal.R.id.no_applications_message);
//每个近期任务的点击事件监听
for (TextView b: mIcons) {
b.setOnClickListener(this);
}
}
/**
* Handler for user clicks. If a button was clicked, launch the corresponding activity.
*/
public void onClick(View v) {
for (TextView b: mIcons) {
if (b == v) {
// prepare a launch intent and send it
//b.getTag得到从ActivityManager取得的每一个任务的intent信息
Intent intent = (Intent)b.getTag();
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
try {
getContext().startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.w("Recent", "Unable to launch recent task", e);
}
}
break;
}
}
dismiss();
}
/**
* Set up and show the recent activities dialog.
显示近期任务对话框
*/
@Override
public void onStart() {
super.onStart();
reloadButtons();
if (sStatusBar != null) {
sStatusBar.disable(StatusBarManager.DISABLE_EXPAND);
}
// receive broadcasts
//注册广播,接收ACTION_CLOSE_SYSTEM_DIALOGS的intent。比如当有电话打过来时,dialog会被让位给打电话的ui
getContext().registerReceiver(mBroadcastReceiver, mBroadcastIntentFilter);
mHandler.removeCallbacks(mCleanup);
}
/**
* Dismiss the recent activities dialog.
*/
@Override
public void onStop() {
super.onStop();
if (sStatusBar != null) {
sStatusBar.disable(StatusBarManager.DISABLE_NONE);
}
//
// stop receiving broadcasts
//接触广播
getContext().unregisterReceiver(mBroadcastReceiver);
mHandler.postDelayed(mCleanup, 100);
}
/**
* Reload the 6 buttons with recent activities
* 最重要的方法,如何从ActivityManager中获取到近期任务。并将其信息"注入"到每一个icon(TextView)中。
*/
private void reloadButtons() {
final Context context = getContext();
final PackageManager pm = context.getPackageManager();
final ActivityManager am = (ActivityManager)
context.getSystemService(Context.ACTIVITY_SERVICE);
final List recentTasks =
am.getRecentTasks(MAX_RECENT_TASKS, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
ActivityInfo homeInfo =
new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)
.resolveActivityInfo(pm, 0);
//近期任务中的图片的创建以及它的选中事件等等图标颜色变化之类的处理。可以想象成我们listview中元素创建以及点击状态变化的处理。
//该类位置:\frameworks\base\policy\src\com\android\internal\policy\impl\IconUtilities.java
IconUtilities iconUtilities = new IconUtilities(getContext());
// Performance note: Our android performance guide says to prefer Iterator when
// using a List class, but because we know that getRecentTasks() always returns
// an ArrayList<>, we'll use a simple index instead.
int index = 0;
int numTasks = recentTasks.size();
//在这里,当前的运行的应用程序一般会第一个加载出来。所以可以在这里将其从近期任务中剔除。方法同剔除
//launcher的方法。
for (int i = 0; i < numTasks && (index < NUM_BUTTONS); ++i) {
final ActivityManager.RecentTaskInfo info = recentTasks.get(i);
// for debug purposes only, disallow first result to create empty lists
if (DBG_FORCE_EMPTY_LIST && (i == 0)) continue;
Intent intent = new Intent(info.baseIntent);
if (info.origActivity != null) {
intent.setComponent(info.origActivity);
}
// Skip the current home activity.
//launcher应用程序不需要显示。
if (homeInfo != null) {
if (homeInfo.packageName.equals(
intent.getComponent().getPackageName())
&& homeInfo.name.equals(
intent.getComponent().getClassName())) {
continue;
}
}
intent.setFlags((intent.getFlags()&~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
| Intent.FLAG_ACTIVITY_NEW_TASK);
final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
if (resolveInfo != null) {
final ActivityInfo activityInfo = resolveInfo.activityInfo;
final String title = activityInfo.loadLabel(pm).toString();
Drawable icon = activityInfo.loadIcon(pm);
if (title != null && title.length() > 0 && icon != null) {
final TextView tv = mIcons[index];
tv.setText(title);
icon = iconUtilities.createIconDrawable(icon);
tv.setCompoundDrawables(null, icon, null, null);
tv.setTag(intent);
tv.setVisibility(View.VISIBLE);
tv.setPressed(false);
tv.clearFocus();
++index;
}
}
}
// handle the case of "no icons to show"
mNoAppsText.setVisibility((index == 0) ? View.VISIBLE : View.GONE);
// hide the rest
for (; index < NUM_BUTTONS; ++index) {
mIcons[index].setVisibility(View.GONE);
}
}
/**
* This is the listener for the ACTION_CLOSE_SYSTEM_DIALOGS intent. It's an indication that
* we should close ourselves immediately, in order to allow a higher-priority UI to take over
* (e.g. phone call received).
*/
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
String reason = intent.getStringExtra(PhoneWindowManager.SYSTEM_DIALOG_REASON_KEY);
if (! PhoneWindowManager.SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {
dismiss();
}
}
}
};
}
至此Home键的按键事件已经剖析完成 。在Android的整体按键事件中只有Power键和Home键是比较特殊的按键,因为其他按键像Menu,Search等,在framework里都分发给了上层应用,也就是说,上层应用可以通过 onKey()函数捕获并屏蔽该按键的操作,但是对于Power键其在InputManager中将其处理交给了PowerManagerService.java,只有长按事件在PhoneWindowManager.java中有处理,故上层无法通过任何方法监听屏蔽Power按键,而对于Home键则较为复杂。接下来着重说明。
重新回到PhoneWindowManager.java的interceptKeyBeforeDispatching()方法,分析该方法中的关于Home的设置选项。该部分源代码为展讯平台SC8825 Android4.0的代码。
if (keyCode == KeyEvent.KEYCODE_HOME) {
// If we have released the home key, and didn't do anything else
// while it was pressed, then it is time to go home!
if (mHomePressed && !down) {
mHomePressed = false;
if (!canceled) {
// If an incoming call is ringing, HOME is totally disabled.
// (The user is already on the InCallScreen at this point,
// and his ONLY options are to answer or reject the call.)
boolean incomingRinging = false;
boolean isOffhook = false;
try {
ITelephony telephonyService = getTelephonyService();
if (telephonyService != null) {
incomingRinging = telephonyService.isRinging();
isOffhook = telephonyService.isOffhook();
}
} catch (RemoteException ex) {
Log.w(TAG, "RemoteException from getPhoneInterface()", ex);
}
if(isOffhook) {
Intent intent = new Intent("com.android.phone.PHONE_FOREGROUND");
intent.putExtra("isforeground", false);
mContext.sendBroadcast(intent);
}
if (incomingRinging) {
Log.i(TAG, "Ignoring HOME; there's a ringing incoming call.");
} else {
launchHomeFromHotKey();
}
} else {
Log.i(TAG, "Ignoring HOME; event canceled.");
}
return -1;
}
// If a system window has focus, then it doesn't make sense
// right now to interact with applications.
WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
if (attrs != null) {
final int type = attrs.type;
//单独设置允许屏蔽Home键的代码,为工厂测试的APK应用可以捕获Home按键事件并屏蔽
//此处根据包名判断
//let factorytest app process home key
if ((null != attrs.packageName) && (attrs.packageName.startsWith("com.spreadst.validationtools"))){
return 0;
}
//此处允许WindowManager.LayoutParams类型为TYPE_KEYGUARD
//和 TYPE_KEYGUARD_DIALOG可以捕获Home事件,该接口是为系统锁屏留的
//锁屏时需要屏蔽Home按键的,所以在Android 2.3平台我们可以通过为自己的Activity
//设置 TYPE_KEYGUARD 或TYPE_KEYGUARD_DIALOG即可,重写onAttachedToWindow
//但是在Android 4.0中该方法不适用
if (type == WindowManager.LayoutParams.TYPE_KEYGUARD
|| type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {
// the "app" is keyguard, so give it the key
return 0;
}
final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
for (int i=0; i
针对Android2.3的代码可以通过getWindow().setType(TYPE_KEYGUARD_DIALOG)属性可以屏蔽Home键,但是此方法不适用与Android 4.0,对于android4.X,网上也有各种方案,但是并不通用,其方法是否能成功与该手机的android内核有关,即framework的interceptKeyBeforeDispatching方法。或者直接修改PhoneWindowManager的interceptKeyBeforeDispatching方法添加对应的应用包名即可。
至此分析结束。