Android's software lock logic implementation software comes with lock screen logic screen lock logic implementation

This is the first time I write Weibo, and I will write it from now on in order to repeat the same mistakes in the past and also for the future commemoration

The software WeChat Alipay that has appeared now

The lock screen they implemented is very simple. It is very simple to judge by one activity.

But if an application has N activities and N pages, how can it be realized? Let’s take a look now.


 * Author     : Byron
For reprint, please indicate the source

1. Lock logic

(1) The program started by loop detection in service

(2) Broadcast reception

(3) If it is your own program, it will pop up

(4) If the input is correct, if the program runs to the background and starts to detect when it is running, it will pop up

The purpose of LaunchMode="SingleInstance" is to override the application 

Insufficient only for security guards below 5.0 including the current 360 and Tencent

My article was created at 11:10:54 on January 13, 2016

Now discuss the lack of locks in 5.0

Take 360 ​​and Tencent's security guard's software lock

Existing bug: the application cannot be locked when it is opened, and it is often overwritten by the software when the lock screen page pops up, because the SingleInstance above 5.0 cannot cover the application

2: All versions 4.0-xx gesture lock will be invalid. The reproduction condition is

(1) Open the 360 ​​or Tencent key and select the locked software to open it

(2) When the lock screen pops up, press the Home button to charge the Recent, remove it, and then open the locked software and find that the software lock is invalid. Reason: The software is killed, and the service will also be stopped for about 2 minutes. If there is enough memory, the service will be restarted.


The above deficiencies are enough to show that the current software lock has almost been scarred and depressed by the revision. .


But I am currently implementing the support of version 4.0-xx of this software

Let me be cruel. I wrote a few days of service to delete the plan to realize the page.

oncreate -onstart-onResume-onpuse-onstop-ondestory


onstart - Check whether the software lock is enabled and pop up if there is one

When everything is onstop, notify the software lock next time


Sorry, this is not possible. This is just an ideal dream. If the functions are so easy to realize, why do people need to realize it?


现在分析下逻辑吧

软件启动过程为Splash界面-主界面-主界面中可能有n个activity 

分析:splash界面启动的时候不需要开启手势锁 原因:这时候开启 可能会被主页面覆盖掉手势锁  所以手势锁启动条件1 if(activity instanceOf SplashActivity) return;

2,splashActivity到-MainActicity-过程  

splashActivity周期oncrate-onstart-onpuse-onstop-ondestory-MainActicity oncreate-start,,其实不是这样的

实际为 splashActivity-onpuse 

MainActicity-oncreate 

MainActicity-onstart 

splashActivity-stop 

splashActivity-desotory

这是进入一个activity 如果是不可见判断 是否下次Oncreate打开手势锁呢?

有点异想天开了 因为打开一个activity最后会走onstop

那么从二级页面退到一级页面又会经历什么周期呢  ,如果是手势锁开启它的周期会不会影响整个周期呢 如果是手势锁开启 输入正确 他也会走Onstop 又该怎么办呢 设置手势锁呢

哈哈 想必你我都烦躁了

我还是直接上代码吧  你们拿来用 有闲心的就看看 里面还是很复杂的 

主要构建逻辑

 分析任务栈--来判断是否开启  如果需要开启 开启之前的条件还有 是否打开手势锁 是否已经输入了  输入正确就没必要在弹出


首先创建 APPLication

 
 
Application 注释我就不写了 看不到的可以直接联系我Q10562080

private static Handler mainHandler;

private static Context mContext;

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

   mainHandler = new Handler();
   this.mContext=getApplicationContext();
 
public static Context getAppContext() {
   return mContext;
}

public static Handler getHandler(){
   return mainHandler;
}
private void initGesture(){
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
      this.registerActivityLifecycleCallbacks( new ActivityLifecycleCallbacks() {

         @Override
         public void onActivityCreated(Activity activity, Bundle savedInstanceState){

            Log.e("onActivityCreated",activity.getClass().getSimpleName());
            MyStack.addActivity(activity);
            GestureManager.getPassCodeManager().onActivityCreated(activity);

         }

         @Override
         public void onActivityStarted(Activity activity) {
            GestureManager.getPassCodeManager().onActivityStarted(activity);
            Log.e("onActivityStarted",activity.getClass().getSimpleName());
         }

         @Override
         public void onActivityResumed(Activity activity) {
         }

         @Override
         public void onActivityPaused(Activity activity) {
         }

         @Override
         public void onActivityStopped(Activity activity) {
            GestureManager.getPassCodeManager().onActivityStopped(activity);
         }

         @Override
         public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

         }

         @Override
         public void onActivityDestroyed(Activity activity) {

            Log.e("onActivityDestroyed",activity.getClass().getSimpleName());
            MyStack.removeActivity(activity);
            GestureManager.getPassCodeManager().onActivityDestroyed(activity);


         }

      });
   }
}
2 任务栈创建

import android.app.Activity;
import android.util.Log;


import java.util.LinkedList;

/**
 * Description:
 * Copyright  : Copyright (c) 2015
 * Company    : 望京soho
 * Author     : Byron
 * Date       : 2016/1/12 15:50
 */
public class MyStack {
    private static final LinkedList<Activity> activityList = ObjectUtil.newLinkedList();
    public static boolean HasActivityForward(Activity activity){
        for (int i=0;i<activityList.size();i++){
            Log.e("content",activity.getClass().getSimpleName());

            if(activityList.get(i)==activity){
                Log.e("activityList.size()",activityList.size()+"");
                try {
                    activityList.get(i+1);
                }catch (Exception e){
                    return false;

                }
            }
        }
        return  true;
    }
    public static void addActivity(Activity activity) {
        activityList.add(activity);
    }
    public static void removeActivity(Activity activity) {
        activityList.remove(activity);
    }
}


3 实现逻辑 在Manager中
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.PowerManager;
import android.util.Log;
import android.view.WindowManager;
HubActivity;
主页的页面
Splashctivity;
闪屏的页面
GestureVerifyActivity //
中锁屏的页面
import java.util.HashSet;
import java.util.Set;

/**
 * Description:
 * Copyright  : Copyright (c) 2015
 * Company    : 望京soho
 * Author     : Byron
 * Date       : 2016/1/11 16:08
 */
public class GestureManager {
    private static final Set<Class> sExemptOfPasscodeActivites;
    private  boolean GestureClock=true;
    private  Handler hanlder;
    private MyRunnable runnable=new MyRunnable();
    static {
        sExemptOfPasscodeActivites = new HashSet<Class>();
        sExemptOfPasscodeActivites.add(GestureVerifyActivity.class);
    }

    public static GestureManager mGestureManagerInstance = null;

    public static GestureManager getPassCodeManager() {
        if (mGestureManagerInstance == null) {
            mGestureManagerInstance = new GestureManager();

        }
        return mGestureManagerInstance;
    }
    protected GestureManager() {
        hanlder=MamApp.getHandler();
    };

    public void onActivityCreated(Activity activity) {
        if (passCodeIsEnabled()) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        } else {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
        }
    }

    public void onActivityStarted(Activity activity) {

        Log.e("onActivityStarted",activity.getClass().getSimpleName());
        if(passCodeIsEnabled()){
            hanlder.removeCallbacks(runnable);
        }else{
            return;
        }
        if(sExemptOfPasscodeActivites.contains(activity.getClass())||activity instanceof SplashScreenActivity){
            return;
        }
        if(GestureClock){
     
     //||!(activity instanceof SplashScreenActivity)&& ActivityStack.getInstance().get
            GestureClock=false;
            Intent i = new Intent(MamApp.getAppContext(), GestureVerifyActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(i);
            GestureVerifyActivity.isClock=true;
            return;
        }
    }

    public void onActivityStopped(Activity activity) {
        if(sExemptOfPasscodeActivites.contains(activity.getClass())){
            return;
        }
        PowerManager powerMgr = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
        if (passCodeIsEnabled() && powerMgr != null && !powerMgr.isScreenOn()) {
            activity.moveTaskToBack(true);
        }
        if(passCodeIsEnabled()){
            if(MyStack.HasActivityForward(activity)){
                ActivityStack.getInstance().getActivityListLength();
                hanlder.removeCallbacks(runnable);
            }else{
                if(! GestureVerifyActivity.isClock){
                    Log.e("开始准备锁屏","开始准备锁屏");
                    hanlder.postDelayed(runnable,60*2*1000);
                }
            }
        }
    }
    public void onActivityDestroyed(Activity activity) {
        if(passCodeIsEnabled()&&!(activity instanceof SplashScreenActivity)){
            hanlder.removeCallbacks(runnable);
            Log.e("取消准备锁屏","取消准备锁屏");
            GestureClock=false;
        }
        if(activity instanceof HubActivity){
            GestureClock=true;
        }
    }
    private class MyRunnable implements Runnable{
        @Override
        public void run() {
                GestureClock=true;
        }
    }
    private boolean passCodeIsEnabled() {
//        SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(MamApp.getAppContext());
        SharedPreferences appPrefs = MamApp.getAppContext().getSharedPreferences("Setting", Context.MODE_PRIVATE);
        return (appPrefs.getBoolean("gestrueState", false));
    }

}


Guess you like

Origin blog.csdn.net/Angle_Byron/article/details/50509236