标签:android 通过广播接听home键 广播注册 广播销毁
Android 通过广播接听Home键,这个方式是比较好用的一种方式
1.自定义广播
public class BroadcastReceiverEx extends 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键 Constants.isHome = true; //ToastUtil.showToast(context, "按了home键" + isHome); }else if(TextUtils.equals(reason, SYSTEM_HOME_KEY_LONG)){ //长按home键,显示最近使用的程序列表 } } } }
2.注册广播
//HOME键监听注册 registerReceiver(new BroadcastReceiverEx(), new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
@Override protected void onRestart() { super.onRestart(); if (Constants.isHome) { // ToastUtil.showToast(context, ""+Constants.isHome); Constants.isHome = false; //按了HOME键重启的操作 } }
/** 监听HOME键 */ public static boolean isHome = false;
@Override public void onLowMemory() { // TODO Auto-generated method stub super.onLowMemory(); unregisterReceiver(broadcastReceiverEx); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:android 通过广播接听home键 广播注册 广播销毁
原文地址:http://blog.csdn.net/pcaxb/article/details/47752685