Android利用广播监听按下HOME和电源键
package cc.testhome; import cc.testhome.HomeKeyObserver.OnHomeKeyListener; import cc.testhome.PowerKeyObserver.OnPowerKeyListener; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 利用广播监听Home键的按下和长按Home键 * 利用广播监听电源键的按下(关闭屏幕) * * 参考资料: * 1 http://blog.csdn.net/q445697127/article/details/8432513 * 2 http://blog.csdn.net/watt520/article/details/18959897 * 3 http://blog.csdn.net/lfdfhl/article/details/9903693 * Thank you very much */ public class MainActivity extends Activity { private HomeKeyObserver mHomeKeyObserver; private PowerKeyObserver mPowerKeyObserver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init() { mHomeKeyObserver = new HomeKeyObserver(this); mHomeKeyObserver.setHomeKeyListener(new OnHomeKeyListener() { @Override public void onHomeKeyPressed() { System.out.println("----> 按下Home键"); } @Override public void onHomeKeyLongPressed() { System.out.println("----> 长按Home键"); } }); mHomeKeyObserver.startListen(); ////////////////////////////////////////// mPowerKeyObserver = new PowerKeyObserver(this); mPowerKeyObserver.setHomeKeyListener(new OnPowerKeyListener() { @Override public void onPowerKeyPressed() { System.out.println("----> 按下电源键"); } }); mPowerKeyObserver.startListen(); } @Override protected void onDestroy() { super.onDestroy(); mHomeKeyObserver.stopListen(); ////////////////////////////////////////// mPowerKeyObserver.stopListen(); } }
package cc.testhome; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; public class HomeKeyObserver { private Context mContext; private IntentFilter mIntentFilter; private OnHomeKeyListener mOnHomeKeyListener; private HomeKeyBroadcastReceiver mHomeKeyBroadcastReceiver; public HomeKeyObserver(Context context) { this.mContext = context; } //注册广播接收者 public void startListen(){ mIntentFilter=new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); mHomeKeyBroadcastReceiver=new HomeKeyBroadcastReceiver(); mContext.registerReceiver(mHomeKeyBroadcastReceiver, mIntentFilter); System.out.println("----> 开始监听"); } //取消广播接收者 public void stopListen(){ if (mHomeKeyBroadcastReceiver!=null) { mContext.unregisterReceiver(mHomeKeyBroadcastReceiver); System.out.println("----> 停止监听"); } } // 对外暴露接口 public void setHomeKeyListener(OnHomeKeyListener homeKeyListener) { mOnHomeKeyListener = homeKeyListener; } // 回调接口 public interface OnHomeKeyListener { public void onHomeKeyPressed(); public void onHomeKeyLongPressed(); } //广播接收者 class HomeKeyBroadcastReceiver extends BroadcastReceiver{ final String SYSTEM_DIALOG_REASON_KEY = "reason"; //按下Home键 final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"; //长按Home键 final String SYSTEM_DIALOG_REASON_RECENT_APPS = "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_DIALOG_REASON_KEY); if (reason != null && mOnHomeKeyListener != null) { if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) { mOnHomeKeyListener.onHomeKeyPressed(); } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) { mOnHomeKeyListener.onHomeKeyLongPressed(); } } } } } }
package cc.testhome; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; public class PowerKeyObserver { private Context mContext; private IntentFilter mIntentFilter; private OnPowerKeyListener mOnPowerKeyListener; private PowerKeyBroadcastReceiver mPowerKeyBroadcastReceiver; public PowerKeyObserver(Context context) { this.mContext = context; } //注册广播接收者 public void startListen(){ mIntentFilter=new IntentFilter(Intent.ACTION_SCREEN_OFF); mPowerKeyBroadcastReceiver=new PowerKeyBroadcastReceiver(); mContext.registerReceiver(mPowerKeyBroadcastReceiver, mIntentFilter); System.out.println("----> 开始监听"); } //取消广播接收者 public void stopListen(){ if (mPowerKeyBroadcastReceiver!=null) { mContext.unregisterReceiver(mPowerKeyBroadcastReceiver); System.out.println("----> 停止监听"); } } // 对外暴露接口 public void setHomeKeyListener(OnPowerKeyListener powerKeyListener) { mOnPowerKeyListener = powerKeyListener; } // 回调接口 public interface OnPowerKeyListener { public void onPowerKeyPressed(); } //广播接收者 class PowerKeyBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_SCREEN_OFF)) { mOnPowerKeyListener.onPowerKeyPressed(); } } } }
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- main.xml--> <textview android:layout_centerinparent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="利用广播监听Home键和电源键"> </textview></relativelayout>