zoukankan      html  css  js  c++  java
  • android home键

    引用:http://www.linuxidc.com/Linux/2012-01/51332.htm

    当我们从Home点击ShortCut图标启动一个应用程序后,这个应用程序打开了很多个Activity,假设顺序为A,B,C,然后我们按Home键,再次从桌面用图标启动这个应用程序,我们会发现显示的是刚才的C,而不是A。这里我们普遍的想法是按Home键是让程序退到后台,然后让桌面显示出来。那么我们就来看看Home键到底是怎么回事。

    在Framework中我们找到源码,我们首先在interceptKeyBeforeDispatching这个方法中找到Home按键代码如下

    [java]

    1. // If the HOME button is currently being held, then we do special   
    2.        // chording with it.   
    3.        if (mHomePressed) {  
    4.              
    5.            // If we have released the home key, and didn't do anything else   
    6.            // while it was pressed, then it is time to go home!   
    7.            if (keyCode == KeyEvent.KEYCODE_HOME) {  
    8.                if (!down) {  
    9.                    mHomePressed = false;  
    10.                      
    11.                    if (!canceled) {  
    12.                        // If an incoming call is ringing, HOME is totally disabled.   
    13.                        // (The user is already on the InCallScreen at this point,   
    14.                        // and his ONLY options are to answer or reject the call.)   
    15.                        boolean incomingRinging = false;  
    16.                        try {  
    17.                            ITelephony phoneServ = getPhoneInterface();  
    18.                            if (phoneServ != null) {  
    19.                                incomingRinging = phoneServ.isRinging();  
    20.                            } else {  
    21.                                Log.w(TAG, "Unable to find ITelephony interface");  
    22.                            }  
    23.                        } catch (RemoteException ex) {  
    24.                            Log.w(TAG, "RemoteException from getPhoneInterface()", ex);  
    25.                        }  
    26.          
    27.                        if (incomingRinging) {  
    28.                            Log.i(TAG, "Ignoring HOME; there's a ringing incoming call.");  
    29.                        } else {  
    30.                            launchHomeFromHotKey();  
    31.                        }  
    32.                    } else {  
    33.                        Log.i(TAG, "Ignoring HOME; event canceled.");  
    34.                    }  
    35.                }  
    36.            }  
    37.              
    38.            return true;  
    39.        }  

    这里就是按Home键时执行的方法我们找到

    [java]
    1. launchHomeFromHotKey();  
    2.   /** 
    3.      * A home key -> launch home action was detected.  Take the appropriate action 
    4.      * given the situation with the keyguard. 
    5.      */  
    6.     void launchHomeFromHotKey() {  
    7.         if (mKeyguardMediator.isShowingAndNotHidden()) {  
    8.             // don't launch home if keyguard showing   
    9.         } else if (!mHideLockScreen && mKeyguardMediator.isInputRestricted()) {  
    10.             // when in keyguard restricted mode, must first verify unlock   
    11.             // before launching home   
    12.             mKeyguardMediator.verifyUnlock(new OnKeyguardExitResult() {  
    13.                 public void onKeyguardExitResult(boolean success) {  
    14.                     if (success) {  
    15.                         try {  
    16.                             ActivityManagerNative.getDefault().stopAppSwitches();  
    17.                         } catch (RemoteException e) {  
    18.                         }  
    19.                         sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY);  
    20.                         startDockOrHome();  
    21.                     }  
    22.                 }  
    23.             });  
    24.         } else {  
    25.             // no keyguard stuff to worry about, just launch home!   
    26.             try {  
    27.                 ActivityManagerNative.getDefault().stopAppSwitches();  
    28.             } catch (RemoteException e) {  
    29.             }  
    30.             sendCloseSystemWindows(SYSTEM_DIALOG_REASON_HOME_KEY);  
    31.             startDockOrHome();  
    32.         }  
    33.     }  

    再进入startDockOrHome();

    [java]
    1. void startDockOrHome() {  
    2.        Intent dock = createHomeDockIntent();  
    3.        if (dock != null) {  
    4.            try {  
    5.                mContext.startActivity(dock);  
    6.                return;  
    7.            } catch (ActivityNotFoundException e) {  
    8.            }  
    9.        }  
    10.        mContext.startActivity(mHomeIntent);  
    11.    }  

    这里我们发现,源码中做了车载模式和普通模式Home的区别,在Android2.3原生系统中有车载模式这个应用程式,打开后按Home键我们返回的是车载模式的桌面,而普通情况下按Home返回正常桌面,再来看看mContext.startActivity(mHomeIntent)这个方法,我们发现,Home键也是打开一个activity,不同的是下面的代码

    [java]

    1. mHomeIntent =  new Intent(Intent.ACTION_MAIN, null);  
    2.       mHomeIntent.addCategory(Intent.CATEGORY_HOME);  
    3.       mHomeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK  
    4.       | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);  

    这段代码是在init()方法中,对mHomeIntent的初始化,这个Intent就是跳转到Launcher程序中的配置了 <category android:name="android.intent.category.HOME" />这个属性的Launcher Activity,另外我们还看到两个flag

    [java]
    1. FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_RESET_TASK_IF_NEEDED  

         newTask保证了我们回到之前Launcher所在的栈,reset task if need是设置清理栈标志,保证清除掉该栈除Launcher以外其他activity(在Launcher清单文件中我们还看到android:clearTaskOnLaunch="true"这个属性,后面会描述),这样我们的Launcher当然会在自己单独的Task中,而且android:launchMode="singleTask"这个属性保证不会启动一个新的Launcher。

           通过Launcher启动的其他Activity不会跑到Launcher所在的Task中(后面的文章会提到Launcher启动activity的过程)。所以按Home键是打开了Launcher这个activity并且保证他在自己单独的Task中,其他的activity进入stop状态。

        另外Launcher中打开一个activity直接进入之前打开的Task 而不是新打开一个activity.

  • 相关阅读:
    读书笔记:你就是极客软件开发人员生存指南
    读书笔记:重来 Rework
    敏捷个人2012.1月份线下活动报道:谈谈职业
    敏捷个人2011.12月份线下活动报道:认识自我
    敏友的【敏捷个人】有感(12): 敏友们自发组织的线上思想的碰撞
    敏捷团队:我尽力先做好本职工作是否正确?
    OpenExpressApp:精通 WPF UI Virtualization
    MDSF:发布图形编辑器源码OpenGraphicEditor
    产品管理:用户访谈之道
    敏捷个人架构图
  • 原文地址:https://www.cnblogs.com/sode/p/3003551.html
Copyright © 2011-2022 走看看