[Android开发] 从后台恢复前台界面需要输入密码的demo|监听Home短按长按锁屏

一、效果图

[Android开发] 从后台恢复前台界面需要输入密码的demo|监听Home短按长按锁屏_第1张图片

二、实现原理

通过接收系统广播来判断home按键,广播Application全局,在接收到广播的时候在BaseActivity的onStop里面打开输入密码的对话框,再次回到界面就已经是显示密码框了。

    final String SYSTEM_DIALOG_REASON_KEY = "reason";
final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";   //切换app和长按home
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";  //hone短按回到桌面
final String SYSTEM_DIALOG_REASON_ASSIST = "assist";   //三星部分手机 长按home
final String SYSTEM_DIALOG_REASON_LOCK = "lock";    //锁屏

三、部分源码

BaseApplication.java

package com.tpnet.safenumberdemo;

import android.app.Activity;
import android.app.Application;

/**
 * Created by LITP on 2016/8/31.
 */
public class BaseApplication extends Application {

    //监听器
    private HomeWatcher homeWatcher;

    @Override
    public void onCreate() {
        super.onCreate();

        //注册按键广播监听
        initBroadcard();
    }

    /**
     * 初始化广播
     */
    private void initBroadcard(){
        homeWatcher = new HomeWatcher(this);
        homeWatcher.startWatch();
    }

    /**
     * BaseActivity的onResume里面设置监听
     * @param activity 当前所在的activity
     */
    public void setListener(Activity activity){
        homeWatcher.setOnHomePressedListener((HomeWatcher.OnHomePressedListener) activity);
    }

}

BaseActivity.java

package com.tpnet.safenumberdemo;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by LITP on 2016/8/31.
 */
public abstract class BaseActivity extends Activity implements HomeWatcher.OnHomePressedListener {

    public BaseApplication mApplication;

    private PassDialog passDialog;

    private enum Step{
        HIDE,SHOW
    }

    Step step = Step.HIDE;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(setView());

        mApplication = getBaseApplication();

        initPassDialog();

        initListener();

        initData();


    }


    protected abstract void initListener();

    protected abstract void initData();

    public abstract int setView();

    /**
     * 获取Application
     * @return
     */
    public BaseApplication getBaseApplication() {
        if (null == mApplication) {
            mApplication = (BaseApplication) getApplication();
        }
        return mApplication;
    }

    /**
     * 初始化密码框
     */
    protected void initPassDialog(){
        passDialog = new PassDialog(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtils.e("onResume");
        getBaseApplication().setListener(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
        LogUtils.e("onStop");
        if(step == Step.SHOW){
            passDialog.show();
            step = Step.HIDE;
        }

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        LogUtils.e("onRestart");
    }



    @Override
    public void onHomeClick() {
        LogUtils.e("onHomeClick");
        step = Step.SHOW;
    }

    @Override
    public void onHomeLongClick() {
        LogUtils.e("onHomeLongClick");
        step = Step.SHOW;
    }

    @Override
    public void onLockScreen() {
        LogUtils.e("onLockScreen");
        step = Step.SHOW;
        passDialog.show();
    }


}

HomeWatcher.java

package com.tpnet.safenumberdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * Home 短按长按 锁屏监听封装
 *
 * @author Litp
 *
 */
public class HomeWatcher {

    static final String TAG = HomeWatcher.class.getClass().getSimpleName();
    private Context mContext;
    private IntentFilter mFilter;
    private OnHomePressedListener mListener;
    private InnerRecevier mRecevier;

    // 回调接口
    public interface OnHomePressedListener {

        /**
         * 短按Home按键
         */
        void onHomeClick();

        /**
         * 长按Home 或 切换程序
         */
        void onHomeLongClick();

        /**
         * 锁屏
         */
        void onLockScreen();

    }

    public HomeWatcher(Context context) {
        mContext = context;
        mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        mRecevier = new InnerRecevier();
    }

    /**
     * 设置监听
     *
     * @param listener
     */
    public void setOnHomePressedListener(OnHomePressedListener listener) {
        mListener = listener;

    }

    /**
     * 开始监听,注册广播
     */
    public void startWatch() {
        if (mRecevier != null) {
            mContext.registerReceiver(mRecevier, mFilter);
        }
    }

    /**
     * 停止监听,注销广播
     */
    public void stopWatch() {
        if (mRecevier != null) {
            mContext.unregisterReceiver(mRecevier);
        }
    }

    /**
     * 广播接收者
     */
    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";
        final String SYSTEM_DIALOG_REASON_ASSIST = "assist";
        final String SYSTEM_DIALOG_REASON_LOCK = "lock";
        @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) {
                    //LogUtils.e("action:" + action + ",reason:" + reason);
                    if (mListener != null) {
                        if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
                            // 短按home键
                            mListener.onHomeClick();
                        } else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {
                            // 长按Home键 或者 activity切换键
                            mListener.onHomeLongClick();
                        }else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) {
                            // 锁屏
                            mListener.onLockScreen();
                        } else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {
                            // samsung 长按Home键,测试了三星s4,这个没执行,还是返回了recentapps
                            LogUtils.e("assist");
                            mListener.onHomeLongClick();
                        }
                    }
                }
            }
        }
    }
}

四、其他方法讨论

还有一种想法,是直接在BaseActivity里面的onStop回调方法里面 判断app是否在前台,不在前台就显示密码框,在前台(如:Activity切换)就不显示。。

本人实验证明: 按Home按键是可以的,但是 切换app就不行了,切换app的时候判断是否在前台是返回真。还有这方法,锁屏也不支持。

还有朋友有好的方法,请留言,谢谢。

demo下载地址:
http://download.csdn.net/detail/niubitianping/9618523

你可能感兴趣的:(Android-Road)