Android Home键监听

来源:互联网 发布:dota27.07知乎 编辑:程序博客网 时间:2024/06/10 16:13

Android Home键监听

有时候我们在我们需要监听Home键的事件,关闭整个程序或者做其他事情,我接手的几个蓝牙项目都是要监听Home键事件,并彻底退出程序,防止程序在后台一直进行数据交互。

监听Back键,我们都知道重写onBackPressed方法就可以了。但是监听Home键就有点麻烦了。

我们可以注解掉Back键,让它不起作用,但是Home键目前只能监听还不能注解掉。

方法一:onUserLeaveHint方法

实际不可行的方法

  /**     * Home触发的回调方法     * 这个方法会在onSaveInstanceState之前执行,根据api的解释,这个方法还比较合适的。     * 但是有个缺点,比如页面处于不可见的情况下也会回调这个方法,页面跳转到其他页面也是会回调     * 简单点说,这个方法和Actvity的onStop方法是一样的,在页面不可见的时候回调     */    @Override    protected void onUserLeaveHint() {        super.onUserLeaveHint();        Log.i(LOG_TAG, "onUserLeaveHint");        finish();    }

onUserLeaveHint方法,虽然可以监听Home键的事件,但是跳转到其他页面这个方法也是回调,所以不符合需求。
这个方法还有一个缺点,就是首次打开程序时,页面显示的时候也会回调这个方法。

方法二:监听广播Action:Intent.ACTION_CLOSE_SYSTEM_DIALOGS

1.创建一个定义的广播接收者

package com.example.liwenzhi.homelistendemo;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 = " TAG HomeReceiver";    private static final String SYSTEM_DIALOG_REASON_KEY = "reason";    //action内的某些reason    private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";//home键旁边的最近程序列表键    private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";//按下home键    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();//        App app = (App) context.getApplicationContext();        Log.i(LOG_TAG, "onReceive: action: " + action);        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {//Action            // 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键旁边的显示最近的程序的按钮                // 长按Home键 或者 activity切换键                Log.i(LOG_TAG, "long press home key or activity switch");            } else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) {  // 锁屏,似乎是没有反应,监听Intent.ACTION_SCREEN_OFF这个Action才有用                Log.i(LOG_TAG, "lock");            } else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {   // samsung 长按Home键                Log.i(LOG_TAG, "assist");            }        }    }}

这个广播接收者可以写成内部类,也可以是在其他包里面。

2.监听Home键的关键代码

package com.example.liwenzhi.homelistendemo;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;public class MainActivity extends AppCompatActivity {    String LOG_TAG = "TAG";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        registerHomeKeyReceiver(this);    }  @Override    protected void onDestroy() {        super.onDestroy();        Log.i(LOG_TAG, "onDestroy");        unregisterHomeKeyReceiver(this);    }    //自定义的广播接收者    private HomeWatcherReceiver mHomeKeyReceiver = null;    //注册广播接收者,监听Home键    private void registerHomeKeyReceiver(Context context) {        Log.i(LOG_TAG, "registerHomeKeyReceiver");        mHomeKeyReceiver = new HomeWatcherReceiver();        IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);        context.registerReceiver(mHomeKeyReceiver, homeFilter);    }    //取消监听广播接收者    private void unregisterHomeKeyReceiver(Context context) {        Log.i(LOG_TAG, "unregisterHomeKeyReceiver");        if (null != mHomeKeyReceiver) {            context.unregisterReceiver(mHomeKeyReceiver);        }    }}

通过上面的代码就能实现Home键的监听,并做相应的操作,如果跳转到其他页面,只要没有关闭这个页面对象,还是在监听Home键的。

上面自定义的广播接收者可以写一个回调接口来监听Home键,也可以把自定义的的广播接收者类写成Activity的内部类。

共勉:人生的高度不是决定于你失败过多少次,失败得有多惨,而是你最终能成功的高度。

原创粉丝点击