一般的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:launchMode="singleTask" 和 onNewIntent(Intent intent)两个特性,现总结一下经验:
android:launchMode="singleTask" 配置在 Mainifest 中,它保证了栈中此Activity总是只有一个,无论你启动它多少次;
onNewIntent(Intent intent) 是Override Activity的父类方法,只有仅在点Home键退出Activity而再次启动新的Intent进来才被调用到;
它们两结合使用,可以做到监听home键(仅当发起新的Intent)。
代码如下:
Manifest.xml 
- <activity android:name=".OnNewIntentDemo"
- android:launchMode="singleTask"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:mimeType="video/*" />
- </intent-filter>
- </activity>
Activity 中
- @Override
- protected void onNewIntent(Intent intent) {
- if(DEBUG) Log.i(TAG, "onNewIntent ~~~~~~~ intent = "+intent);
- super.onNewIntent(intent);
- }
注意: 当按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)
; }
 
                    
                
 

 
                
            
        
 浙公网安备 33010602011771号
浙公网安备 33010602011771号