Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android通過Intent.ACTION_CLOSE_SYSTEM_DIALOGS監聽Home按鍵消息

Android通過Intent.ACTION_CLOSE_SYSTEM_DIALOGS監聽Home按鍵消息

編輯:關於Android編程

Android對屏幕下方常用的四個按鍵消息處理是不一致的:

1、搜索按鍵的消息在onKeyDown或者onKeyUp中接收;

2、菜單按鍵的消息在onCreateOptionsMenu、onKeyDown或onKeyUp方法中接收;

3、返回按鍵的消息可以在onBackPressed、onKeyDown或onKeyUp方法中接收。

 

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	switch( keyCode ){
    	case KeyEvent.KEYCODE_BACK:{
    		
    	}
    	break;
    	case KeyEvent.KEYCODE_MENU:{
    		
    	}
    	break;
    	case KeyEvent.KEYCODE_SEARCH:{
    		
    	}
    	break;
    	default:{
    		
    	}
    	break;
    	}
    	return super.onKeyDown(keyCode, event);
    }
    
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
    	switch( keyCode ){
    	case KeyEvent.KEYCODE_BACK:{
    		
    	}
    	break;
    	case KeyEvent.KEYCODE_MENU:{
    		
    	}
    	break;
    	case KeyEvent.KEYCODE_SEARCH:{
    		
    	}
    	break;
    	default:{
    		
    	}
    	break;
    	}
    	return super.onKeyUp(keyCode, event);
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}
    
    @Override
    public void onBackPressed() {
    	super.onBackPressed();
    }
對於Home按鍵消息的處理,既不能通過onKeyDown、onKeyUp接收到,android也沒有提供專有的方法接收按鍵消息,個人估計home按鍵算是一個app異常信息處理的後門,比如ANR後,按其它按鈕沒有比按Home按鍵好使,所以android為了能夠提供更好的用戶體驗,沒有提供供用戶監聽home按鍵消息的方法。

 

注:網上提供了各種各樣監聽Home按鍵消息的方法,但只有這個比較好使。

 

但辦法總是有的,在每次點擊Home按鍵時都會發出一個action為Intent.ACTION_CLOSE_SYSTEM_DIALOGS的廣播,它是關閉系統Dialog的廣播,我們可以通過注冊它來監聽Home按鍵消息,我自定義了一個home按鍵監聽工具類,代碼如下,使用說明參見類名上方的使用說明:

 

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * Home按鍵監聽類
 * 使用說明:
 * 1、初始化HomeListen
 * HomeListen homeListen = new HomeListen( this );
 * homeListen.setOnHomeBtnPressListener( new setOnHomeBtnPressListener(){
 * 		@Override
 * 		public void onHomeBtnPress( ){
 * 			// 按下Home按鍵回調
 * 		}
 * 		
 * 		@Override
 * 		public void onHomeBtnLongPress( ){
 * 			// 長按Home按鍵回調
 * 		}
 * });
 * 
 * 2、在onResume方法中啟動HomeListen廣播:
 * homeListen.start();
 * 
 * 3、在onPause方法中停止HomeListen廣播:
 * homeListen.stop( );
 * */
public class HomeListen {
	public HomeListen(Context context) {
		mContext = context;
		mHomeBtnReceiver = new HomeBtnReceiver( );
		mHomeBtnIntentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
	}
	
	public void setOnHomeBtnPressListener( OnHomeBtnPressLitener onHomeBtnPressListener ){
		mOnHomeBtnPressListener = onHomeBtnPressListener;
	}
	
	public void start( ){
		mContext.registerReceiver( mHomeBtnReceiver, mHomeBtnIntentFilter );
	}
	
	public void stop( ){
		mContext.unregisterReceiver( mHomeBtnReceiver );
	}
	
	class HomeBtnReceiver extends BroadcastReceiver{
		@Override
		public void onReceive(Context context, Intent intent) {
			receive( context, intent );
		}
	}
	
	private void receive(Context context, Intent intent){
		String action = intent.getAction();
		if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
			String reason = intent.getStringExtra( "reason" );
			if (reason != null) {
				if( null != mOnHomeBtnPressListener ){
					if( reason.equals( "homekey" ) ){
						// 按Home按鍵
						mOnHomeBtnPressListener.onHomeBtnPress( );
					}else if( reason.equals( "recentapps" ) ){
						// 長按Home按鍵
						mOnHomeBtnPressListener.onHomeBtnLongPress( );
					}
				}
			}
		}
	}
	
	public interface OnHomeBtnPressLitener{
		public void onHomeBtnPress( );
		public void onHomeBtnLongPress( );
	}
	
	private Context mContext = null;
	private IntentFilter mHomeBtnIntentFilter = null;
	private OnHomeBtnPressLitener mOnHomeBtnPressListener = null;
	private HomeBtnReceiver mHomeBtnReceiver = null;
}

在Activity中做如下調用即可:

 

 

public class HomeListenActivity extends Activity {
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_home_listen_layout);  
        initHomeListen( );
    }
    
    @Override
    protected void onResume( ) {
    	super.onResume();
    	mHomeListen.start( );
    }
  
    @Override  
    protected void onPause() {  
        super.onPause();
        mHomeListen.stop( );
    }
    
    private void initHomeListen( ){
    	mHomeListen = new HomeListen( this );
    	mHomeListen.setOnHomeBtnPressListener( new OnHomeBtnPressLitener( ) {
			@Override
			public void onHomeBtnPress() {
				showToast( "按下Home按鍵!" );
			}
			
			@Override
			public void onHomeBtnLongPress() {
				showToast( "長按Home按鍵!" );
			}
		});
    }
    
    private void showToast( String toastInfoStr ){
    	Toast.makeText( this, toastInfoStr, Toast.LENGTH_LONG ).show( );
    }

    private HomeListen mHomeListen = null;
}

 

Demo程序下載地址:Android監聽Home按鍵

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved