监听方法Android之Home键监听封装

本文是一篇关于监听方法的帖子

    尽人皆知,我们监听返回键事件,无法是上面两个方法:

@Override
	public void onBackPressed() {
		//do something
		//super.onBackPressed();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			//do something
		}
		return super.onKeyDown(keyCode, event);
	}

    

    于是乎,我们理所当然的想到,监听Home键的方法是:if(keyCode == KeyEvent.KEYCODE_HOME)。但实际上,这样是行不通的,因为此时home键的消息在framework层就已被拦截,所以,我们在应用中通过此方法是无法监听到Home键的消息的。那么我们如何捕获Home键事件,并作出响应处理呢?其实系统还是给我们发出广播的,上面,就跟大家分享一下,封装好了的监听Home键的方法:

    HomeWatcher类:

/**
 * Home键监听封装
 * 
 * @author way
 * 
 */
public class HomeWatcher {

	static final String TAG = "HomeWatcher";
	private Context mContext;
	private IntentFilter mFilter;
	private OnHomePressedListener mListener;
	private InnerRecevier mRecevier;

	// 回调接口
	public interface OnHomePressedListener {
		public void onHomePressed();

		public void onHomeLongPressed();
	}

	public HomeWatcher(Context context) {
		mContext = context;
		mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
	}

	/**
	 * 设置监听
	 * 
	 * @param listener
	 */
	public void setOnHomePressedListener(OnHomePressedListener listener) {
		mListener = listener;
		mRecevier = new InnerRecevier();
	}

	/**
	 * 开始监听,注册广播
	 */
	public void startWatch() {
		if (mRecevier != null) {
			mContext.registerReceiver(mRecevier, mFilter);
		}
	}

	/**
	 * 停止监听,注销广播
	 */
	public void stopWatch() {
		if (mRecevier != null) {
			mContext.unregisterReceiver(mRecevier);
		}
	}

	/**
	 * 广播接收者
	 */
	class InnerRecevier extends BroadcastReceiver {
		final String SYSTEM_DIALOG_REASON_KEY = "reason";
		final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
		final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
		final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
				String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
				if (reason != null) {
					Log.e(TAG, "action:" + action + ",reason:" + reason);
					if (mListener != null) {
						if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
							// 短按home键
							mListener.onHomePressed();
						} else if (reason
								.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
							// 长按home键
							mListener.onHomeLongPressed();
						}
					}
				}
			}
		}
	}
}
    每日一道理
毅力,是千里大堤一沙一石的凝聚,一点点地累积,才有前不见头后不见尾的壮丽;毅力,是春蚕吐丝一缕一缕的环绕,一丝丝地坚持,才有破茧而出重见光明的辉煌; 毅力,是远航的船的帆,有了帆,船才可以到达成功的彼岸。

    

    接下来,我们需要在应用中开启监听,上面是以在Activity中的应用为例:

/**
 * 应用HomeWatcher小例
 * 
 * @author way
 * 
 */
public class MainActivity extends Activity {
	private static final String TAG = "MainActivity";
	private HomeWatcher mHomeWatcher;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mHomeWatcher = new HomeWatcher(this);
		mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
			@Override
			public void onHomePressed() {
				Log.e(TAG, "onHomePressed");
			}

			@Override
			public void onHomeLongPressed() {
				Log.e(TAG, "onHomeLongPressed");
			}
		});
		mHomeWatcher.startWatch();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mHomeWatcher.stopWatch();// 在onPause中停止监听,不然会报错的。
	}
}

文章结束给大家分享下程序员的一些笑话语录: 腾讯总舵主马化腾,有人曾经戏称如果在Z国选举总统,马化腾一定当选,因为只要QQ来一个弹窗”投马总,送Q币”即可。

你可能感兴趣的:(android)