/** * 模拟按home键 * 程序退到后台运行 * @param context */ private void imitatePressHome(Context context) { Intent intent = new Intent(Intent.ACTION_MAIN); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addCategory(Intent.CATEGORY_HOME); context.startActivity(intent); } /** * 注册home键监听 * @param context */ private void registerHomeReceiver(Context context) { context.registerReceiver(mHomeKeyEventReceiver, new IntentFilter( Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); } /** * home键处理 */ private BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver() { String SYSTEM_REASON = "reason"; String SYSTEM_HOME_KEY = "homekey"; String SYSTEM_HOME_KEY_LONG = "recentapps"; @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_REASON); if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) { //表示按了home键,程序到了后台 Toast.makeText(context, "home pressed", 1000).show(); }else if(TextUtils.equals(reason, SYSTEM_HOME_KEY_LONG)){ //表示长按home键,显示最近使用的程序列表 Toast.makeText(context, "home long pressed", 1000).show(); } } } }; /** * home键屏蔽 */ @Override public void onAttachedToWindow() { // TODO Auto-generated method stub this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD); super.onAttachedToWindow(); }
原文地址:http://blog.csdn.net/tangnengwu/article/details/39610597