捕捉物理按键


KeyEvent.KEYCODE_POWER :电源键(常量值为26)
  1. @Override  
  2.     public boolean onKeyDown(int keyCode, KeyEvent event)  
  3.     {  
  4.         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   返回键里面处理你的业务逻辑 
 
 
 
 

Android 如何监听返回键,弹出一个退出对话框

标签: androiddialogbuttonclass
 分类:
 

Android 如何监听返回键点击事件,并创建一个退出对话框,

防止自己写的应用程序不小心点击退出键而直接退出。自己记录下这个简单的demo,备用。

注:如下代码当时是从网上copy过来的,现在忘了它出自哪个原作者了,在此说声抱歉。

源码如下:

[html] view plain copy
 
  1. public class BackKeyTest extends Activity  
  2. {  
  3.     /** Called when the activity is first created. */  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState)  
  6.     {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.   
  10.     }  
  11.       
  12.     @Override  
  13.     public boolean onKeyDown(int keyCode, KeyEvent event)  
  14.     {  
  15.         if (keyCode == KeyEvent.KEYCODE_BACK )  
  16.         {  
  17.             // 创建退出对话框  
  18.             AlertDialog isExit = new AlertDialog.Builder(this).create();  
  19.             // 设置对话框标题  
  20.             isExit.setTitle("系统提示");  
  21.             // 设置对话框消息  
  22.             isExit.setMessage("确定要退出吗");  
  23.             // 添加选择按钮并注册监听  
  24.             isExit.setButton("确定", listener);  
  25.             isExit.setButton2("取消", listener);  
  26.             // 显示对话框  
  27.             isExit.show();  
  28.   
  29.         }  
  30.           
  31.         return false;  
  32.           
  33.     }  
  34.     /**监听对话框里面的button点击事件*/  
  35.     DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener()  
  36.     {  
  37.         public void onClick(DialogInterface dialog, int which)  
  38.         {  
  39.             switch (which)  
  40.             {  
  41.             case AlertDialog.BUTTON_POSITIVE:// "确认"按钮退出程序  
  42.                 finish();  
  43.                 break;  
  44.             case AlertDialog.BUTTON_NEGATIVE:// "取消"第二个按钮取消对话框  
  45.                 break;  
  46.             default:  
  47.                 break;  
  48.             }  
  49.         }  
  50.     };    
  51. }  


小结:

Android手机常用的三个键,home键,back键及menu键。
在应用程序里我们经常会对它们经常进行一定的处理,方便用户使用。
首先我们要明确点击三个键时系统干了什么事,
如果没有进行监听处理,
点击home键时,系统默认只执行应用程序的当前显示的Activity的onStop()方法后跳出界面。
而点击back键时,系统默认执行的是应用程序当前Activity的finish()方法后跳出界面。
而点击menu键时,系统默认不进行任何处理。

这里只是一个简单的应用demo,我们可以根据自己的需要设计一个更完美的退出程序对话框。

也可以在监听到返回事件后进行其他处理,等等。

 
 
原文地址:https://www.cnblogs.com/ZhuRenWang/p/Android.html