Back键和Home键监听



 

Back键的监听

  对于Back键的监听比较容易,可以在多个系统回调处拦截,比如在activity的下列方法中都可以收到Back键按下的事件:

复制代码
    @Override
    public void onBackPressed() {
        // super.onBackPressed();//注释掉这行,back键不退出activity

        Log.i(LOG_TAG, "onBackPressed");
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Log.i(LOG_TAG, "dispatchKeyEvent: keyCode -- " + event.getKeyCode());
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.i(LOG_TAG, "onKeyDown: keyCode -- " + keyCode);

        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            Log.i(LOG_TAG, "KeyEvent.KEYCODE_BACK");
            break;
        case KeyEvent.KEYCODE_MENU:
            Log.i(LOG_TAG, "KeyEvent.KEYCODE_MENU");
            break;
        case KeyEvent.KEYCODE_HOME:
            Log.i(LOG_TAG, "KeyEvent.KEYCODE_HOME");
            // 收不到
            break;
        case KeyEvent.KEYCODE_APP_SWITCH:
            Log.i(LOG_TAG, "KeyEvent.KEYCODE_APP_SWITCH");
            // 收不到
            break;
        default:
            break;
        }

        return super.onKeyDown(keyCode, event);

    }
复制代码

 

Home键的广播监听

  对于Home键的监听不是那么容易,因为Home键可以将程序退出放在后台,所以这个事件是直接分发给系统,系统接收到之后做相应处理,Home键的事件不是直接传递到应用里面.所以在上述监听Back键的代码中,相应的回调中是收不到Home键的事件的.

  参考文后的博客链接,对Home键的监听主要通过注册广播接收器实现,拦截让窗口关闭的系统动作,然后根据Intent里面的具体参数,分析当前到底是Home键, 应用切换键,还是其他功能按键.

  接收器实现如下:

复制代码
package com.mengdd.hellohome;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class HomeWatcherReceiver extends BroadcastReceiver {
    private static final String LOG_TAG = "HomeReceiver";
    private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
    private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
    private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
    private static final String SYSTEM_DIALOG_REASON_LOCK = "lock";
    private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.i(LOG_TAG, "onReceive: action: " + action);
        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
            // android.intent.action.CLOSE_SYSTEM_DIALOGS
            String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
            Log.i(LOG_TAG, "reason: " + reason);

            if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
                // 短按Home键
                Log.i(LOG_TAG, "homekey");

            }
            else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {
                // 长按Home键 或者 activity切换键
                Log.i(LOG_TAG, "long press home key or activity switch");

            }
            else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) {
                // 锁屏
                Log.i(LOG_TAG, "lock");
            }
            else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {
                // samsung 长按Home键
                Log.i(LOG_TAG, "assist");
            }

        }
    }

}
复制代码

  注意不同手机的按键不同,所以需要对不同理由做区分.

 

Home键监听广播注册

  广播接收器的注册有两种方式,一种是静态注册,即写在manifest里面声明;另一种是动态注册,即在Java代码里面注册.

  上面对Home键实现监听的这个receiver,静态注册如下:

        <receiver android:name="com.mengdd.hellohome.HomeWatcherReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />
            </intent-filter>
        </receiver>

  但是发现静态注册不起作用,即收不到onReceive回调.

 

  采用动态注册:

复制代码
    private static HomeWatcherReceiver mHomeKeyReceiver = null;

    private static void registerHomeKeyReceiver(Context context) {
        Log.i(LOG_TAG, "registerHomeKeyReceiver");
        mHomeKeyReceiver = new HomeWatcherReceiver();
        final IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);

        context.registerReceiver(mHomeKeyReceiver, homeFilter);
    }

    private static void unregisterHomeKeyReceiver(Context context) {
        Log.i(LOG_TAG, "unregisterHomeKeyReceiver");
        if (null != mHomeKeyReceiver) {
            context.unregisterReceiver(mHomeKeyReceiver);
        }
    }
复制代码

  

  在Activity的onResume和onPause里面分别调用:

复制代码
    @Override
    protected void onResume() {
        super.onResume();

        registerHomeKeyReceiver(this);
    }

    @Override
    protected void onPause() {

        unregisterHomeKeyReceiver(this);
        super.onPause();
    }
复制代码

  当然也可以根据需要在其他合适的时机注册和注销.

Back键的监听

  对于Back键的监听比较容易,可以在多个系统回调处拦截,比如在activity的下列方法中都可以收到Back键按下的事件:


复制代码
@Override
public void onBackPressed() {
// super.onBackPressed();//注释掉这行,back键不退出activity

Log.i(LOG_TAG, "onBackPressed");
}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.i(LOG_TAG, "dispatchKeyEvent: keyCode -- " + event.getKeyCode());
return super.dispatchKeyEvent(event);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i(LOG_TAG, "onKeyDown: keyCode -- " + keyCode);

switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
Log.i(LOG_TAG, "KeyEvent.KEYCODE_BACK");
break;
case KeyEvent.KEYCODE_MENU:
Log.i(LOG_TAG, "KeyEvent.KEYCODE_MENU");
break;
case KeyEvent.KEYCODE_HOME:
Log.i(LOG_TAG, "KeyEvent.KEYCODE_HOME");
// 收不到
break;
case KeyEvent.KEYCODE_APP_SWITCH:
Log.i(LOG_TAG, "KeyEvent.KEYCODE_APP_SWITCH");
// 收不到
break;
default:
break;
}

return super.onKeyDown(keyCode, event);

}

复制代码



Home键的广播监听

  对于Home键的监听不是那么容易,因为Home键可以将程序退出放在后台,所以这个事件是直接分发给系统,系统接收到之后做相应处理,Home键的事件不是直接传递到应用里面.所以在上述监听Back键的代码中,相应的回调中是收不到Home键的事件的.

  参考文后的博客链接,对Home键的监听主要通过注册广播接收器实现,拦截让窗口关闭的系统动作,然后根据Intent里面的具体参数,分析当前到底是Home键, 应用切换键,还是其他功能按键.

  接收器实现如下:


复制代码
package com.mengdd.hellohome;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class HomeWatcherReceiver extends BroadcastReceiver {
private static final String LOG_TAG = "HomeReceiver";
private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
private static final String SYSTEM_DIALOG_REASON_LOCK = "lock";
private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(LOG_TAG, "onReceive: action: " + action);
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
// android.intent.action.CLOSE_SYSTEM_DIALOGS
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
Log.i(LOG_TAG, "reason: " + reason);

if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
// 短按Home键
Log.i(LOG_TAG, "homekey");

}
else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {
// 长按Home键 或者 activity切换键
Log.i(LOG_TAG, "long press home key or activity switch");

}
else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) {
// 锁屏
Log.i(LOG_TAG, "lock");
}
else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {
// samsung 长按Home键
Log.i(LOG_TAG, "assist");
}

}
}

}

复制代码

  注意不同手机的按键不同,所以需要对不同理由做区分.



Home键监听广播注册

  广播接收器的注册有两种方式,一种是静态注册,即写在manifest里面声明;另一种是动态注册,即在Java代码里面注册.

  上面对Home键实现监听的这个receiver,静态注册如下:

<receiver android:name="com.mengdd.hellohome.HomeWatcherReceiver" >
<intent-filter>
<action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />
</intent-filter>
</receiver>

  但是发现静态注册不起作用,即收不到onReceive回调.



  采用动态注册:


复制代码
private static HomeWatcherReceiver mHomeKeyReceiver = null;

private static void registerHomeKeyReceiver(Context context) {
Log.i(LOG_TAG, "registerHomeKeyReceiver");
mHomeKeyReceiver = new HomeWatcherReceiver();
final IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);

context.registerReceiver(mHomeKeyReceiver, homeFilter);
}

private static void unregisterHomeKeyReceiver(Context context) {
Log.i(LOG_TAG, "unregisterHomeKeyReceiver");
if (null != mHomeKeyReceiver) {
context.unregisterReceiver(mHomeKeyReceiver);
}
}

复制代码

  

  在Activity的onResume和onPause里面分别调用:


复制代码
@Override
protected void onResume() {
super.onResume();

registerHomeKeyReceiver(this);
}

@Override
protected void onPause() {

unregisterHomeKeyReceiver(this);
super.onPause();
}

复制代码

  当然也可以根据需要在其他合适的时机注册和注销.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值