Introduction: This article describes how to implement a lock on the application of the function, without root permissions
Some people sometimes have such a demand, small a download software, just the beauty of the software is too tempting and exposed, so he does not want to let others know this is a software, at least do not want to let others open browsing. And this software is not locked, anyone can open, swollen? If you need to enter a password when you open it, then how good! Thus, the application of the program lock produces a
Program locks are not available recently, and Android has this apk a long time ago.
In this issue we come to the harsh how to implement the program lock function
First, let's make it clear what the function of the program we are going to do
1 You can choose a program that needs to be locked
2 You can set a password
3 Can close program lock
as a demonstration here, we'll try to simplify the code
Let's start with the most critical part.
The key point is: How to pop up the password page when the user opens an app?
There's no good way to scan the topactivity in a task.
First, we get the running task first
Mactivitymanager = (Activitymanager) context.getsystemservice ("activity"); // mactivitymanager.getrunningtasks (1); // list<runningtaskinfo>
Getrunningtasks method returns a list, let's see what this list is
Getrunningtasks wrote
Inch
The returned list is ordered, the first one is recent, so we take the first one and get the top activity in this task
ComponentName topactivity = mactivitymanager.getrunningtasks (1). Get (0). topactivity;
Topactivity incredibly is componentname type, the following things will be done, get the package name and class name
ComponentName topactivity = Mactivitymanager.getrunningtasks (1).Get(0). topactivity; String PackageName=Topactivity.getpackagename (); String ClassName=Topactivity.getclassname (); LOG.V (TAG,"PackageName"+PackageName); LOG.V (TAG,"ClassName"+className);if(Testpackagename.equals (PackageName)&&testclassname.equals (ClassName)) {Intent Intent=NewIntent (); Intent.setclassname ("com.example.locktest","com.example.locktest.PasswordActivity"); Intent.setflags (Intent.flag_activity_new_task); Mcontext.startactivity (intent);}
Since I did not choose the program this step, so I fixed an application to do the test, the choice is the HTC Note application
" com.htc.notes " "com.htc.notes.collection.NotesGridViewActivity";
Let's think about when this code is executed.
Open an application, the system will not send the broadcast, we cannot listen directly, so here we take the policy of timed scanning
Here is just a simple implementation, after which we discuss the optimization
We take the method of checking the task every second, use a timer here, and use the same handler to achieve
Private Timer Mtimer; Private void Starttimer () { ifnull) { new Timer (); New Locktask (this); 0L 1000L ); }}
Here, in fact, our key code is done.
The following is a complete strip code, note: We only focus on the pop-up lock interface, the other part of the implementation (such as the end of the article)
Task, responsible for checking the task and popping the password page when appropriate
Public classLocktask extends TimerTask { Public StaticFinal String TAG ="Locktask"; PrivateContext Mcontext; String Testpackagename="com.htc.notes"; String Testclassname="com.htc.notes.collection.NotesGridViewActivity"; PrivateActivitymanager Mactivitymanager; PublicLocktask (Context context) {Mcontext=context; Mactivitymanager= (Activitymanager) context.getsystemservice ("Activity"); } @Override Public voidrun () {componentname topactivity= Mactivitymanager.getrunningtasks (1).Get(0). topactivity; String PackageName=Topactivity.getpackagename (); String ClassName=Topactivity.getclassname (); LOG.V (TAG,"PackageName"+PackageName); LOG.V (TAG,"ClassName"+className); if(Testpackagename.equals (PackageName)&&testclassname.equals (ClassName)) {Intent Intent=NewIntent (); Intent.setclassname ("com.example.locktest","com.example.locktest.PasswordActivity"); Intent.setflags (Intent.flag_activity_new_task); Mcontext.startactivity (Intent); } }}
Lockservice, responsible for performing scheduled tasks, canceling tasks, etc.
Public classLockservice extends Service {PrivateTimer Mtimer; Public StaticFinalintforeground_id =0; Private voidStarttimer () {if(Mtimer = =NULL) {Mtimer=NewTimer (); Locktask Locktask=NewLocktask ( This); Mtimer.schedule (Locktask,0L,1000L); } } Publicibinder onbind (Intent Intent) {return NULL; } Public voidonCreate () {super.oncreate (); Startforeground (foreground_id,NewNotification ()); } Public intOnstartcommand (Intent Intent,intFlagsintStartid) {Starttimer (); returnSuper.onstartcommand (Intent, flags, Startid); } Public voidOnDestroy () {Stopforeground (true); Mtimer.cancel (); Mtimer.purge (); Mtimer=NULL; Super.ondestroy (); }}
Mainactivity, test, as an application entry, start service (in the product, we can start the service in receiver).
Public class Mainactivity extends Activity { publicvoid onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); StartService (new Intent (This, lockservice. Class));} }
passwordactivity, Password page, very rough, no check password logic, self-realization
Remember to rewrite the onbackpressed function, or press the return key ... You know
Public classPasswordactivity extends Activity {Private StaticFinal String TAG ="passwordactivity"; Button OKButton; EditText Passwordedittext; PrivateBoolean mfinish =false; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.password); Passwordedittext=(EditText) Findviewbyid (R.id.password); OKButton=(Button) Findviewbyid (R.id.ok); Okbutton.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (View v) {String password=Passwordedittext.gettext (). toString (); LOG.V (TAG,"Password"+password); Mfinish=true; Finish (); } }); } Public voidonbackpressed () {} Public voidOnPause () {super.onpause (); if(!mfinish) {Finish (); } }}
XML is not posted here, remember to add permissions
<uses-permission android:name="android.permission.GET_TASKS"/>
For other parts of the program, here is a brief explanation.
Select the app to lock it in the section
1 List All programs in the system (you can also freely play, such as filter out the original application)
2 Select, then deposit the database (of course, there is also a cancellation function, remember to delete data from the database)
Program Lock Master Switch
You can use Sharedpreference to set a Boolean switch
Now, when I want to open the HTC Note app, it will pop up the password page when I unlock, press home to go back to the desktop, press and hold home, click Note, or the password box will pop up
Because it is checked once per second, there may be a little delay and you can set it to 500 milliseconds, but the more frequently you use it, the more resources you will consume.
The above code I get topactivity after checking its Baonian and class name, so only when the specified page is opened, the password lock will pop up
For example, I applied encryption to the gallery, but the user is editing the text message, when it wants to send MMS, so he through the text into the gallery ...
For some users of some of the requirements, this is not tolerated, at this time, we simply modify the logic of the judgment: only check the package name, the package name is consistent pop-up password lock, so perfect
The program lock, I'm analyzing this.
One last sentence
When using the program lock, you long press home, found that the program lock also appeared in the "recent tasks", swollen do ... Set android:excludefromrecents= "True" to this activity
Android Security Issue program lock