KeyEvent.KEYCODE_POWER :电源键(常量值为26)
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event)
- {
- if (keyCode == KeyEvent.KEYCODE_POWER)
@Override
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn) {
if (!mSystemBooted) {
// If we have not yet booted, don't let key events do anything.
return 0;
}
final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
final boolean canceled = event.isCanceled();
final int keyCode = event.getKeyCode();
//Log.d(TAG, "keyCode="+keyCode);
final boolean isInjected = (policyFlags & WindowManagerPolicy.FLAG_INJECTED) != 0;
// If screen is off then we treat the case where the keyguard is open but hidden
// the same as if it were open and in front.
// This will prevent any keys other than the power button from waking the screen
// when the keyguard is hidden by another activity.
final boolean keyguardActive = (mKeyguardDelegate == null ? false :
(isScreenOn ?
mKeyguardDelegate.isShowingAndNotHidden() :
mKeyguardDelegate.isShowing()));
if (keyCode == KeyEvent.KEYCODE_POWER) {
policyFlags |= WindowManagerPolicy.FLAG_WAKE;
}
final boolean isWakeKey = (policyFlags & (WindowManagerPolicy.FLAG_WAKE
| WindowManagerPolicy.FLAG_WAKE_DROPPED)) != 0;
if (DEBUG_INPUT) {
Log.d(TAG, "interceptKeyTq keycode=" + keyCode
+ " screenIsOn=" + isScreenOn + " keyguardActive=" + keyguardActive
+ " policyFlags=" + Integer.toHexString(policyFlags)
+ " isWakeKey=" + isWakeKey);
}
if (down && (policyFlags & WindowManagerPolicy.FLAG_VIRTUAL) != 0
&& event.getRepeatCount() == 0) {
performHapticFeedbackLw(null, HapticFeedbackConstants.VIRTUAL_KEY, false);
}
// Basic policy based on screen state and keyguard.
// FIXME: This policy isn't quite correct. We shouldn't care whether the screen
// is on or off, really. We should care about whether the device is in an
// interactive state or is in suspend pretending to be "off".
// The primary screen might be turned off due to proximity sensor or
// because we are presenting media on an auxiliary screen or remotely controlling
// the device some other way (which is why we have an exemption here for injected
// events).
int result;
if ((isScreenOn && !mHeadless) || (isInjected && !isWakeKey)) {
// When the screen is on or if the key is injected pass the key to the application.
result = ACTION_PASS_TO_USER;
} else {
// When the screen is off and the key is not injected, determine whether
// to wake the device but don't pass the key to the application.
result = 0;
if (down && isWakeKey && isWakeKeyWhenScreenOff(keyCode)) {
result |= ACTION_WAKE_UP;
}
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{
if(down)
{
//Log.d(TAG, "phoneIsInUse="+phoneIsInUse());
//Log.d(TAG, "getInt="+Settings.System.getInt(mContext.getContentResolver(),"emergency_call", 0));
//if(Settings.System.getInt(mContext.getContentResolver(),"emergency_call", 0) != 0&&!phoneIsInUse())
if(!phoneIsInUse())
{
//Log.d(TAG, "phoneIsInUse "+"if......");
mHandler.postDelayed(mVolumnUpLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
}
}
else
{
//Log.d(TAG, "phoneIsInUse "+"else......");
mHandler.removeCallbacks(mVolumnUpLongPress);
}
}
keyCode==KeyEvent.KEYCODE_BACK 返回键里面处理你的业务逻辑
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags, boolean isScreenOn) {
if (!mSystemBooted) {
// If we have not yet booted, don't let key events do anything.
return 0;
}
final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
final boolean canceled = event.isCanceled();
final int keyCode = event.getKeyCode();
//Log.d(TAG, "keyCode="+keyCode);
final boolean isInjected = (policyFlags & WindowManagerPolicy.FLAG_INJECTED) != 0;
// If screen is off then we treat the case where the keyguard is open but hidden
// the same as if it were open and in front.
// This will prevent any keys other than the power button from waking the screen
// when the keyguard is hidden by another activity.
final boolean keyguardActive = (mKeyguardDelegate == null ? false :
(isScreenOn ?
mKeyguardDelegate.isShowingAndNotHidden() :
mKeyguardDelegate.isShowing()));
if (keyCode == KeyEvent.KEYCODE_POWER) {
policyFlags |= WindowManagerPolicy.FLAG_WAKE;
}
final boolean isWakeKey = (policyFlags & (WindowManagerPolicy.FLAG_WAKE
| WindowManagerPolicy.FLAG_WAKE_DROPPED)) != 0;
if (DEBUG_INPUT) {
Log.d(TAG, "interceptKeyTq keycode=" + keyCode
+ " screenIsOn=" + isScreenOn + " keyguardActive=" + keyguardActive
+ " policyFlags=" + Integer.toHexString(policyFlags)
+ " isWakeKey=" + isWakeKey);
}
if (down && (policyFlags & WindowManagerPolicy.FLAG_VIRTUAL) != 0
&& event.getRepeatCount() == 0) {
performHapticFeedbackLw(null, HapticFeedbackConstants.VIRTUAL_KEY, false);
}
// Basic policy based on screen state and keyguard.
// FIXME: This policy isn't quite correct. We shouldn't care whether the screen
// is on or off, really. We should care about whether the device is in an
// interactive state or is in suspend pretending to be "off".
// The primary screen might be turned off due to proximity sensor or
// because we are presenting media on an auxiliary screen or remotely controlling
// the device some other way (which is why we have an exemption here for injected
// events).
int result;
if ((isScreenOn && !mHeadless) || (isInjected && !isWakeKey)) {
// When the screen is on or if the key is injected pass the key to the application.
result = ACTION_PASS_TO_USER;
} else {
// When the screen is off and the key is not injected, determine whether
// to wake the device but don't pass the key to the application.
result = 0;
if (down && isWakeKey && isWakeKeyWhenScreenOff(keyCode)) {
result |= ACTION_WAKE_UP;
}
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{
if(down)
{
//Log.d(TAG, "phoneIsInUse="+phoneIsInUse());
//Log.d(TAG, "getInt="+Settings.System.getInt(mContext.getContentResolver(),"emergency_call", 0));
//if(Settings.System.getInt(mContext.getContentResolver(),"emergency_call", 0) != 0&&!phoneIsInUse())
if(!phoneIsInUse())
{
//Log.d(TAG, "phoneIsInUse "+"if......");
mHandler.postDelayed(mVolumnUpLongPress, ViewConfiguration.getGlobalActionKeyTimeout());
}
}
else
{
//Log.d(TAG, "phoneIsInUse "+"else......");
mHandler.removeCallbacks(mVolumnUpLongPress);
}
}
keyCode==KeyEvent.KEYCODE_BACK 返回键里面处理你的业务逻辑
分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
Android 如何监听返回键点击事件,并创建一个退出对话框,
防止自己写的应用程序不小心点击退出键而直接退出。自己记录下这个简单的demo,备用。
注:如下代码当时是从网上copy过来的,现在忘了它出自哪个原作者了,在此说声抱歉。
源码如下:
- public class BackKeyTest extends Activity
- {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event)
- {
- if (keyCode == KeyEvent.KEYCODE_BACK )
- {
- // 创建退出对话框
- AlertDialog isExit = new AlertDialog.Builder(this).create();
- // 设置对话框标题
- isExit.setTitle("系统提示");
- // 设置对话框消息
- isExit.setMessage("确定要退出吗");
- // 添加选择按钮并注册监听
- isExit.setButton("确定", listener);
- isExit.setButton2("取消", listener);
- // 显示对话框
- isExit.show();
- }
- return false;
- }
- /**监听对话框里面的button点击事件*/
- DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()
- {
- public void onClick(DialogInterface dialog, int which)
- {
- switch (which)
- {
- case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序
- finish();
- break;
- case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框
- break;
- default:
- break;
- }
- }
- };
- }
小结:
Android手机常用的三个键,home键,back键及menu键。
在应用程序里我们经常会对它们经常进行一定的处理,方便用户使用。
首先我们要明确点击三个键时系统干了什么事,
如果没有进行监听处理,
点击home键时,系统默认只执行应用程序的当前显示的Activity的onStop()方法后跳出界面。
而点击back键时,系统默认执行的是应用程序当前Activity的finish()方法后跳出界面。
而点击menu键时,系统默认不进行任何处理。
这里只是一个简单的应用demo,我们可以根据自己的需要设计一个更完美的退出程序对话框。
也可以在监听到返回事件后进行其他处理,等等。