Android home键的控制

1.禁止home键的使用

这个功能原理不是很明白,目前先套着用,有机会再了解

@Override
    public void onAttachedToWindow() {
        try {
            ICBCPayActivity.this.getWindow().setType(
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onAttachedToWindow();
    }

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

             return true;//禁用返回键
        }
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_MUTE) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

但在通过startactivityForresult启动时,返回结果若处理比较慢,出现遮罩层,此时点击home键仍然可以返回。

2.home键的监听

来源于:https://blog.youkuaiyun.com/rtyfghcvb/article/details/52790979

这是网络上找的例子,home键的监听实际是监听点击home键后的广播Intent.ACTION_CLOSE_SYSTEM_DIALOGS

/**
 * Created by KingLeoric on 2016/10/10.
 */
public class HomeKeyEventBroadcastReceiver extends BroadcastReceiver {

    private static final String SYSTEM_EVENT_REASON = "reason";
    private static final String SYSTEM_HOME_KEY = "homekey";
    private static final String SYSTEM_RECENT_APPS = "recentapps";

    private HomeKeyListenerHelper.HomeKeyListener listener;

    public HomeKeyEventBroadcastReceiver(HomeKeyListenerHelper.HomeKeyListener l) {
        listener = l;
    }

    /**
     * This method is called when the BroadcastReceiver is receiving an Intent
     * broadcast.  During this time you can use the other methods on
     * BroadcastReceiver to view/modify the current result values.  This method
     * is always called within the main thread of its process, unless you
     * explicitly asked for it to be scheduled on a different thread using
     * {@link Context#registerReceiver(BroadcastReceiver,
     * IntentFilter, String, Handler)}. When it runs on the main
     * thread you should
     * never perform long-running operations in it (there is a timeout of
     * 10 seconds that the system allows before considering the receiver to
     * be blocked and a candidate to be killed). You cannot launch a popup dialog
     * in your implementation of onReceive().
     * <p>
     * <p><b>If this BroadcastReceiver was launched through a &lt;receiver&gt; tag,
     * then the object is no longer alive after returning from this
     * function.</b>  This means you should not perform any operations that
     * return a result to you asynchronously -- in particular, for interacting
     * with services, you should use
     * {@link Context#startService(Intent)} instead of
     * {@link Context#bindService(Intent, ServiceConnection, int)}.  If you wish
     * to interact with a service that is already running, you can use
     * {@link #peekService}.
     * <p>
     * <p>The Intent filters used in {@link Context#registerReceiver}
     * and in application manifests are <em>not</em> guaranteed to be exclusive. They
     * are hints to the operating system about how to find suitable recipients. It is
     * possible for senders to force delivery to specific recipients, bypassing filter
     * resolution.  For this reason, {@link #onReceive(Context, Intent) onReceive()}
     * implementations should respond only to known actions, ignoring any unexpected
     * Intents that they may receive.
     *
     * @param context The Context in which the receiver is running.
     * @param intent  The Intent being received.
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == intent) {
            return;
        }
        String action = intent.getAction();
        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
            String reason = intent.getStringExtra(SYSTEM_EVENT_REASON);
            if (null != reason) {
                if (reason.equals(SYSTEM_HOME_KEY)) {
                    //Home key short pressed.
                    if (null != listener) {
                        listener.onHomeKeyShortPressed();
                    }
                } else if (reason.equals(SYSTEM_RECENT_APPS)) {
                    //Home key long pressed.
                    if (null != listener) {
                        listener.onHomeKeyLongPressed();
                    }
                }
            }
        }
    }
}

public class HomeKeyListenerHelper {

    private Context context;
    private BroadcastReceiver receiver;

    public HomeKeyListenerHelper(Context ctx) {
        context = ctx;
    }

    public void registerHomeKeyListener(HomeKeyListener l) {
        if (null != context) {
            registerListener(l);
        }
    }

    private void registerListener(HomeKeyListener l) {
        receiver = new HomeKeyEventBroadcastReceiver(l);
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        context.registerReceiver(receiver, intentFilter);
    }

    public void unregisterHomeKeyListener() {
        if (null != context && null != receiver) {
            context.unregisterReceiver(receiver);
        }
    }

    public interface HomeKeyListener {
        public void onHomeKeyShortPressed();
        public void onHomeKeyLongPressed();
    }

}

Activity使用:

homeKeyListenerHelper = new HomeKeyListenerHelper(ICBCPayActivity.this);
        homeKeyListenerHelper
                .registerHomeKeyListener(new HomeKeyListenerHelper.HomeKeyListener() {
                    @Override
                    public void onHomeKeyShortPressed() {
                        Toast.makeText(ICBCPayActivity.this, "您点击了home键",
                                Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onHomeKeyLongPressed() {
                        Toast.makeText(ICBCPayActivity.this, "您长按了home键",
                                Toast.LENGTH_SHORT).show();
                    }
                });

@Override
    protected void onDestroy() {
        super.onDestroy();
        homeKeyListenerHelper.unregisterHomeKeyListener();
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值