按键监听

来源:互联网 发布:android 美团外卖源码 编辑:程序博客网 时间:2024/05/17 07:33

home键:

public static BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver() {  //监听home键,如果按了home键则退出长截屏模式    String SYSTEM_REASON = "reason";      String SYSTEM_HOME_KEY = "homekey";      String RECENT_APPS = "recentapps";             @Override      public void onReceive(Context context, Intent intent) {      Tools.log(TAG, "mHomeKeyEventReceiver   onReceive ");    if (mScreenService.isSavingPicture) {    Tools.log(TAG, "mHomeKeyEventReceiver   onReceive   mScreenService.isSavingPicture==true  now return");    return;}        String action = intent.getAction();          Tools.log(TAG, "  intent.getAction(); =====" + intent.getAction());        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {              String reason = intent.getStringExtra(SYSTEM_REASON);              Tools.log(TAG, "   intent.getStringExtra(SYSTEM_REASON) == " + intent.getStringExtra(SYSTEM_REASON));            if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {                   //表示按了home键,程序到了后台              Tools.log(TAG, "mHomeKeyEventReceiver onReceive Home clicked  stopSelfBySelf");                mScreenService.stopSelfBySelf();            }else if (TextUtils.equals(reason, RECENT_APPS)) {            Tools.log(TAG, "mHomeKeyEventReceiver onReceive recent apps  clicked   stopSelfBySelf");                mScreenService.stopSelfBySelf();}        }       }  }; public static BroadcastReceiver PhoneStatReceiver = new BroadcastReceiver() {          @Override      public void onReceive(Context context, Intent intent) {              //如果是拨打电话              if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){                          //                    incomingFlag = false;                      String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);                                              }else{                                              //如果是来电                      TelephonyManager tm =                           (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);                                                                    switch (tm.getCallState()) {                    case TelephonyManager.CALL_STATE_RINGING:                      if (mScreenService.isSavingPicture) {                    Tools.log(TAG, "phoneStatReceiver call is comming  mScreenService.isSavingPicture=="+mScreenService.isSavingPicture);                    return;                    }                    Tools.log(TAG, "phoneStatReceiver call is comming stopSelfBySelf");                    mScreenService.stopSelfBySelf();                    }               }      }  } ;

熄屏监听:这个只能动态注册才能接收到

/**     * Broadcast Action: Sent when the device goes to sleep and becomes non-interactive.     * <p>     * For historical reasons, the name of this broadcast action refers to the power     * state of the screen but it is actually sent in response to changes in the     * overall interactive state of the device.     * </p><p>     * This broadcast is sent when the device becomes non-interactive which may have     * nothing to do with the screen turning off.  To determine the     * actual state of the screen, use {@link android.view.Display#getState}.     * </p><p>     * See {@link android.os.PowerManager#isInteractive} for details.     * </p>     * You <em>cannot</em> receive this through components declared in     * manifests, only by explicitly registering for it with     * {@link Context#registerReceiver(BroadcastReceiver, IntentFilter)     * Context.registerReceiver()}.     *     * <p class="note">This is a protected intent that can only be sent     * by the system.     */    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)    public static final String ACTION_SCREEN_OFF = "android.intent.action.SCREEN_OFF";


public static BroadcastReceiver ScreenOffReceiver = new BroadcastReceiver() {          @Override      public void onReceive(Context context, Intent intent) {              //如果虚拟来电            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {                    if (mScreenService.isSavingPicture) {                    Tools.log(TAG, "FakeCall is comming  mScreenService.isSavingPicture=="+mScreenService.isSavingPicture);                    return;                    }                    Tools.log(TAG, "Screen off  stopSelfBySelf");                    mScreenService.stopSelfBySelf();             }      }  } ;












0 0
原创粉丝点击