Features:1. The password lock is set in the application program, and the password input screen will pop up whenever the user opens the app. 2. When the user hold down the home button, the program is hidden in the background rather than exit, after a period of time, and then restart, also to eject the password input interface; 3. When applied to the foreground, the user presses the Power key, turns off the screen, and then lights up the screen, which also pops up the password entry screen.
Implementation method:1. For function 1, each time the application is launched into the main interface, it is necessary to determine whether to eject the password input interface; 2. The key point is how to determine whether the program is in the foreground and backstage. Judging method:
public static Boolean Isapponforeground (context context) { Activitymanager Activitymanager = (Activitymanager) Context.getapplicationcontext (). Getsystemservice (Context.activity_service); String PackageName = Context.getapplicationcontext (). Getpackagename (); list<runningappprocessinfo> appprocesses = activitymanager.getrunningappprocesses (); if (appprocesses = = null) return false; for (Runningappprocessinfo appprocess:appprocesses) { if (appProcess.processName.equals (PackageName) && Appprocess.importance = = runningappprocessinfo.importance_foreground) { return true; } } return false;}
The main principle is: Get all the processes currently running, based on the process name for each comparison (the default application process name is the package name),and the importance of the current process is importance_foreground, and the process is in the foreground.
Add a Boolean variable misactive = True to the activity and the default is true.
Make a judgment in the onstop of all activity:
protected void OnStop () { super.onstop (); if (!isapponforeground (this)) { misactive = false; }}
In the Onrestart () of the corresponding activity, determine:
protected void Onrestart () { if (!misactive) { misactive = true; Pop-up Password box input interface //... }}
3. Monitor lock screen Broadcast: ACTION_SCREEN_ON registers a broadcast in application:
public static Boolean is_foreground = False;public class Screenactionreceiver extends Broadcastreceiver { @Override c1/>public void OnReceive (context context, Intent Intent) { String action = intent.getaction (); if (Action.equals (intent.action_screen_on)) {// User Light screen //Determine if a pop-up password lock is required if (is_foreground) { // If the application happens to be in the foreground, the password lock interface will pop up } } else if (Action.equals (Intent.action_screen_off)) { //user off screen }}
Register the instance of the Broadcastreceiverd in your own application OnCreate ():
if (Mscreenactionreceiver = = null) { mscreenactionreceiver = new Screenactionreceiver (); Intentfilter screenfilter = new Intentfilter (); Screenfilter.addaction (Intent.action_screen_off); Screenfilter.addaction (intent.action_screen_on); Registerreceiver (Mscreenactionreceiver, screenfilter);}
Note the point:when you receive a screen light notification, to determine whether the application is in the foreground, if not in the foreground, do not drop the password lock interface. at this point the Isapponforeground () method is called, and the discovery returns True, that is, when the program receives the broadcast, the system thinks the application is running to foreground. Workaround: Set a variable in applicationIs_foreground,The activity's OnCreate (), OnStop (), Onrestart () are judged and the value of the variable is set.
Implementation of password lock for Android app