Some people sometimes have such a demand, small a download a software, but the beauty of the software is too temptation and exposure, so he did not want to let others know what it is software, at least do not want to let others open browsing. And this software has no lock, anyone can open, swollen mody do? If you need to enter a password when you open it, how good it is! Thus, the application of the program lock produces
The program lock is not a recent one, and Android has this apk a long time ago.
We're going to be tough on this one. How to Implement program lock function
First, let's make clear what the function of our program is
1 You can choose to lock the program
2 can set the password
3 can close the program lock
As a demo here, we'll try to simplify the code.
Let's start with the most critical part.
The key is: When users open an application, how to eject the password page?
There's no great way to scan the topactivity in a task.
First, we get the task to run first.
Mactivitymanager = (Activitymanager)
context.getsystemservice ("activity");
Mactivitymanager.getrunningtasks (1);//list<runningtaskinfo>
Getrunningtasks method returns a list, let's take a look at what this list is
Getrunningtasks wrote
Return a list of the tasks this are currently running, with the most recent being the I and older after in order.
......
The list returned is ordered, the first one is the nearest, so we take the first one and get the topmost activity in this task
ComponentName topactivity = mactivitymanager.getrunningtasks (1). Get (0). topactivity;
Topactivity incredibly is componentname type, the following thing is good to do, get 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 = new Intent ();
Intent.setclassname ("Com.example.locktest", "com.example.locktest.PasswordActivity");
Intent.setflags (intent.flag_activity_new_task);
Mcontext.startactivity (intent);
}
Since I did not select the program this step, so I fixed an application to do the test, here is the choice of HTC Note application
String testpackagename = "Com.htc.notes";
String testclassname = "com.htc.notes.collection.NotesGridViewActivity";
Now we should think about when this code was executed
Open an application, the system will not send the broadcast, we can not directly monitor, so here we take a timed scan strategy
This is just a simple implementation, and then we'll discuss the optimization
We take the time to check the task once a second, use the timer here, the same as handler can be achieved
Private Timer Mtimer;
private void Starttimer () {
if (Mtimer = = null) {
Mtimer = new Timer ();
Locktask locktask = new Locktask (this);
Mtimer.schedule (Locktask, 0L, 1000L);
}
Here, in fact, our key code has been completed