Android监听Home、电源键

2019-07-13 23:36发布

Android监听Home、电源键 一、Home键监听  先禁止Home键,再在onKeyDown里处理按键值,点击Home键的时候就把程序关闭(此方法在4.0及以上版本中不可用) <uses-permission android:name="android.permission.DISABLE_KEYGUARD">uses-permission   >         @Override    public boolean onKeyDown(int keyCode, KeyEvent event)   { // TODO Auto-generated method stub     if(KeyEvent.KEYCODE_HOME==keyCode)       android.os.Process.killProcess(android.os.Process.myPid());        return super.onKeyDown(keyCode, event);     }       @Override    public void onAttachedToWindow()    { // TODO Auto-generated method stub       this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);       super.onAttachedToWindow();    }           二、电源键监听   首先,我们是要获取系统权限    
 通过监听系统的两个action:   Intent.ACTION_SCREEN_OFF   Intent.ACTION_SCREEN_ON   private final BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {   @Override   public void onReceive(final Context context, final Intent intent) {   final String action = intent.getAction();   if (Intent.ACTION_SCREEN_OFF.equals(action)) {   //退出程序...   }   }   };   onCreate()方法中注册   IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);   registerReceiver(mBatInfoReceiver, filter);   onDestory()方法中解除注册   if(mBatInfoReceiver != null){   try {   unregisterReceiver(mBatInfoReceiver);   } catch (Exception e) {   Log.e(TAG, "unregisterReceiver mBatInfoReceiver failure :"+e.getCause());   }   }   以上简单介绍使用方法,下面实现自定义Home电源键监听(支持4.0及以上或以下)   /**
 * Home键监听
 * @author miaowei
 *
 */
public class HomeKeyListener {
    static final String TAG = "HomeKeyListener";
  private Context mContext;
  private IntentFilter mFilter;
  private OnHomePressedListener mListener;
  private InnerRecevier mRecevier;
  /**
   * Home键监听构造初始化
   * @param context
   */
  public HomeKeyListener(Context context) {
    mContext = context;
    mFilter = new IntentFilter();
    mFilter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    mFilter.addAction(Intent.ACTION_SCREEN_OFF);
    mFilter.addAction(Intent.ACTION_SCREEN_ON);
  
 }
 
 /**
  * 回调接口
  * @author miaowei
  *
  */
  public interface OnHomePressedListener {
 
   /**
    * Home键短按
    */
   public void onHomePressed();
   /**
    * Home键长按
    */
   public void onHomeLongPressed();
   /**
    * 监听电源键/开
    */
   public void onScreenPressed();
   /**
    * 监听电源键/关
    */
   public void offScreenPressed();
  }
 
  /**
   * 设置监听
   * @param listener
   */
 public void setOnHomePressedListener(OnHomePressedListener listener) {
  mListener = listener;
  mRecevier = new InnerRecevier();
 }
 
   /**
  * 开始监听,注册广播
  */
 public void startHomeListener() {
  if (mRecevier != null) {
   mContext.registerReceiver(mRecevier, mFilter);
  }
 }
 
 /**
  * 停止监听,注销广播
  */
 public void stopHomeListener() {
  if (mRecevier != null) {
   mContext.unregisterReceiver(mRecevier);
  }
 }
  /**
   * 广播接收
   * @author miaowei
   *
   */
  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();
      }
     }
    }
   }//监听电源键开
   if (action.equals(Intent.ACTION_SCREEN_OFF)) {
    
    mListener.offScreenPressed();
    
   }else if (action.equals(Intent.ACTION_SCREEN_ON)) {
    
    mListener.onScreenPressed();
   }
  }
  }
}
  在Activity中的使用    public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private HomeKeyListener homeKeyListener;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
 
       homeKeyListener = new HomeKeyListener(this);
       homeKeyStart(); //处理方法
         
       homeKeyListener.startHomeListener(); //开启监听
    }
    /**
  * Home键开始监听
  */
 private void homeKeyStart(){
  
  homeKeyListener.setOnHomePressedListener(new OnHomePressedListener() {
   
   @Override
   public void onHomePressed() {
    
    // 这里获取到home键按下事件
    Log.i("lock", "onHomePressed ========================================");
    
   }
   
   @Override
   public void onHomeLongPressed() {
    
    Log.i("lock", "onHomeLongPressed ========================================");
    
   }      @Override
   public void onScreenPressed() {
    // TODO Auto-generated method stub
    
   }      @Override
   public void offScreenPressed() {
    // TODO Auto-generated method stub
      }
  });
 }
    @Override
    protected void onDestroy() {
        super.onPause(); 
       if(homeKeyListener != null){                  homeKeyListener.stopHomeListener(); //关闭监听           }          
    }
}