zoukankan      html  css  js  c++  java
  • activity 对home 按钮事件的处理

    一般的activity在程序内部是相应不了 home 按钮事件的。只有类似launcher这种activity才能取到相关事件。

    即便如此,在

    public boolean onKeyDown(int keyCode, KeyEvent keyEvent)

    或者  OnKeyListener()中是接受不到此事件的。必须通过接收广播的方法才能接收到:

    import android.content.BroadcastReceiver;
    import android.content.IntentFilter;

    public class xxx extends Activity{

    private final BroadcastReceiver mCloseSystemDialogsReceiver
                 = new CloseSystemDialogsIntentReceiver();

    public void onCreate(Bundle paramBundle){

    … …

      IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
                    registerReceiver(mCloseSystemDialogsReceiver, filter);

    }

        private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                String reason = intent.getStringExtra("reason");
                if (!"homekey".equals(reason)) {
                    Log.v(TAG," ------------ hoemkey down BroadcastReceiver ------------------- ");
                }
             if("homekey".equals(reason)) {
                 intent
                    Log.v(TAG," ------------ homekey.equals(reason)  ------------------- ");//此处能收到home 按键事件
                }
            Log.v(TAG," ------------ hoemkey down BroadcastReceiver  2-------------- ");   
            }
        }

        public void onDestroy()
        {
            super.onDestroy();   
            unregisterReceiver(mCloseSystemDialogsReceiver);
        }

    }

    其实一般是不在上面的点处理home 按钮事件的,而是在:onNewIntent方法中处理,anlauncher的处理会

    会先做个比较。

    @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
         Log.d(TAG, " -----------------------  onNewIntent  " );
            // Close the menu
            if (Intent.ACTION_MAIN.equals(intent.getAction())) {  //在此处处理home 按钮事件
                Log.d(TAG, " -----------------------  onNewIntent :Intent.ACTION_MAIN.equals(intent.getAction() " );
                // also will cancel mWaitingForResult.
                closeSystemDialogs();

                boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
                            != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
                boolean allAppsVisible = isAllAppsVisible();
                if (!mWorkspace.isDefaultScreenShowing()) {
                    mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
                }
                closeAllApps(alreadyOnHome && allAppsVisible);

                final View v = getWindow().peekDecorView();
                if (v != null && v.getWindowToken() != null) {
                    InputMethodManager imm = (InputMethodManager)getSystemService(
                            INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
        else{
            Log.d(TAG, " -----------------------  onNewIntent : ! Intent.ACTION_MAIN.equals(intent.getAction() " );
        }
        }

    ==============================================

    http://blog.csdn.net/adreamer_bj/archive/2010/12/22/6091414.aspx

    android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法

    Android.netXMLBlog

    最近项目开发中用到了android:launchMode="singleTask" 和 onNewIntent(Intent intent)两个特性,现总结一下经验:
    android:launchMode="singleTask" 配置在 Mainifest 中,它保证了栈中此Activity总是只有一个,无论你启动它多少次;
    onNewIntent(Intent intent) 是Override Activity的父类方法,只有仅在点Home键退出Activity而再次启动新的Intent进来才被调用到;
    它们两结合使用,可以做到监听home键(仅当发起新的Intent)。
    代码如下:
    Manifest.xml

    Java代码 收藏代码

    1. <activity android:name=".OnNewIntentDemo"
    2.             android:launchMode="singleTask"
    3.                   android:label="@string/app_name">  
    4.             <intent-filter>  
    5.                 <action android:name="android.intent.action.MAIN" />  
    6.                 <category android:name="android.intent.category.LAUNCHER" />  
    7.             </intent-filter>  
    8.             <intent-filter>  
    9.                 <action android:name="android.intent.action.VIEW" />  
    10.                 <category android:name="android.intent.category.DEFAULT" />  
    11.                 <data android:mimeType="video/*" />  
    12.             </intent-filter>  
    13. </activity> 

    Activity 中

    Java代码 收藏代码

    1. @Override
    2. protected void onNewIntent(Intent intent) {  
    3. if(DEBUG) Log.i(TAG, "onNewIntent ~~~~~~~ intent = "+intent);  
    4. super.onNewIntent(intent);  
    5.     } 

    注意: 当按Home键退出,再长按Home键进入,此时onNewIntent不被访问,因为再次进入的时候没有被发起Intent。

    ==============================================

    http://hi.baidu.com/luyanlong1/blog/item/aef3af2c82ad998f023bf6f7.html

    onNewIntent的应用 Android

    2011年03月07日 星期一 下午 06:03

         onCreate是用来创建一个Activity也就是创建一个窗体,但一个Activty处于任务栈的顶端,若再次调用startActivity去创建它,则不会再次创建。若你想利用已有的Acivity去处理别的Intent时,你就可以利用onNewIntent来处理。在onNewIntent里面就会获得新的Intent.

    @Override

    protected void onNewIntent(Intent intent) {

    // TODO Auto-generated method stub

    super.onNewIntent(intent)

    ;        } 

  • 相关阅读:
    BZOJ1568:[JSOI2008]Blue Mary开公司
    HDU4348:To the moon
    洛谷【P3437】[POI2006]TET-Tetris 3D
    AtCoder Regular Contest 072 E:Alice in linear land
    AtCoder Grand Contest 014 D:Black and White Tree
    洛谷【P2664】树上游戏
    浅谈树分治
    BZOJ3784:树上的路径
    BZOJ2006:[NOI2010]超级钢琴
    BZOJ3648:寝室管理
  • 原文地址:https://www.cnblogs.com/leaven/p/2209015.html
Copyright © 2011-2022 走看看