In the project in order to security considerations need to add graphics to unlock this function, after collecting all aspects of the data to complete the development, the need for a simple description (omit open and close, reset, forget the need)
1. The app needs to be unlocked when the app is re-aroused when it is turned on or placed in the background when the phone lock screen
2. Enter the graphics to unlock when the app is in the background or jumps to a third-party app over 30s and then returns to the app interface
Define a long variable (hidetime) that records time in a custom application to record the time, control the appearance of the graphics Unlock (unlockactivity) in the custom base class activity, and the code is as follows
1 @Override2 protected voidOnStart () {3 //TODO auto-generated Method Stub4 Super. OnStart ();5 LongResumetime =System.currenttimemillis ();6 if(Resumetime-mapplication.hidetime > 30000) {7Sendbroadcast (NewIntent (staticutil.lock_activity));8 }9 }Ten One @Override A protected voidOnresume () { - //TODO auto-generated Method Stub - Super. Onresume (); theIntentfilter Intentfilter =NewIntentfilter (); - intentfilter.addaction (staticutil.finish_activity); - intentfilter.addaction (staticutil.lock_activity); - intentfilter.addaction (intent.action_screen_off); + registerreceiver (Reciver, intentfilter); - + } A at @Override - protected voidOnPause () { - //TODO auto-generated Method Stub - Super. OnPause (); -Mapplication.hidetime =System.currenttimemillis (); - } in - @Override to protected voidOnStop () { + //TODO auto-generated Method Stub - Super. OnStop (); theMapplication.hidetime =System.currenttimemillis (); * } $ Panax Notoginseng @Override - protected voidOnDestroy () { the Unregisterreceiver (reciver); + Super. OnDestroy (); A}
The control that opens the unlockactivity is placed in a custom broadcast receiver, the ACTION is defined as Staticutil.lock_activity, and the broadcast reception of the lock screen (Action_screen_off) is turned on, tested now
lock Screen when the first entry is OnPause and then receive the broadcast, the intermediate time difference of not more than 1s, so here to make the following judgment, if the lock screen time minus activity into onpause more than 1s can think the user is
The lock screen that the app is placed in the background before execution, or the lock screen while the app is running. This does not open unlockactivity directly on the lock screen because it occurs on some machines (e.g. mi2, Nubian z5s, etc.) as long as the user unlocks the screen
The application's unlockactivity is always started bug, while some other models will not (such as Huawei Glory 3c, Samsung S4, etc.)
1 Public classLotterybroadcastreciverextendsBroadcastreceiver {2 @Override3 Public voidOnReceive (Context context, Intent Intent) {4 //TODO auto-generated Method Stub5String action =intent.getaction ();6 if(Action.equals (staticutil.finish_activity)) {7 finish ();8}Else if(Action.equals (staticutil.lock_activity)) {9 if(MApplication.mLockPatternUtils.savedPatternExists ()) {TenIntent lockintent =NewIntent (lotteryactivity. This, OneUnlockactivity.class); A Lockintent.addflags (Intent.flag_activity_clear_top -|intent.flag_activity_new_task); - startactivity (lockintent); the } -}Else if(Action.equals (Intent.action_screen_off)) { - LongResumetime =System.currenttimemillis (); - if(Resumetime-mapplication.hidetime < 1000) { +Sendbroadcast (NewIntent (staticutil.lock_activity)); -}Else { +Mapplication.hidetime-= 30000; A } at } - } -}
Finally, after unlockactivity completes the correct unlocking process, executes the finish () method and refreshes the hidden Time (Hidetime), and in order to prevent the application of its own activity opened to enter the OnPause, OnStop appear lock screen situation, Rewrite the user click the Back button, Onactivityresult, finish and other methods, in the hidden Time (hidetime) was refreshed
Unlockactivity is a reference to http://blog.csdn.net/vrix/article/details/39003433 only minor adjustments to the business.
android-Graphics Unlock