Android shield specified activity startup (Program lock)

Source: Internet
Author: User

Some security management classes have the program lock function. The general function is as follows: the security management program lists all applications in the system, the user selects the programs to be locked, and sets the password. In this case, running those programs will jump to a permission page, requiring you to enter a password for access, or not to access and launch.

How to implement it?

The first idea is to write a service monitoring activity stack. If there is a change at the top of the stack, judge whether it is a protected program. If yes, intent turns to its own defined authentication and authorization activity; no, then do nothing.

Find it. The SDK does not provide a callback method or an API such as an event or broadcast. It can only be detected while.

The second method is used to find all services and aidl in the source code, and no callback or broadcast is found. In addition to modifying the source code, activitymanager cannot provide such functions externally.

Native layer? Not considered for the moment.

Now let's write the sample code. Who knows how to detect the top status of the activity stack without using the while method? Thank you.

Service Code:

@ Override
Public void onstart (intent, int startid ){
Super. onstart (intent, startid );

// We will obtain the content of the current activity stack through the activity management service.
Final activitymanager AM = (activitymanager) getsystemservice (activity_service );

// This intent can be written to death, because it is always switched to this
Final intent mintent = new intent ();
// The flag must be a new task. Think carefully about the difference between the service and the activity.
Mintent. setflags (intent. flag_activity_new_task );

// Needless to say
Mintent. setclass (getapplicationcontext (), lockactivity. Class );

// A thread that keeps Detecting
Asynctask taskwatcher = new asynctask (){

@ Override
Protected object doinbackground (object... Params ){

// Use this while as the dog.
While (true ){

/** We only need to get one runningtasks. In general, it will be obtained from the top of the stack according to the number of incoming
There is always an activity in a set and it will not be blank. Please rest assured
*/
String name = aM. getrunningtasks (1). Get (0). topactivity. getpackagename ();

/** Determine whether the current activity package is protected
Of course, what is actually reading data? Get a set
This set is usually used to list all installed APK packages in the system in advance, and let the user choose the desired
*/
If (pakagename. Equals (name )){

/** Turn to the target and set your own intent
* You can enter a password in an input box.
*
* In the target activity, the Return key must be blocked. Because the stack top of the previous task is returned
* It will return here again, and it makes no sense to write code and run it on the other side. If the password is correct
* After the activity is verified, the expected activity is automatically returned.
*
* Verify the policy and the validity period of the verification, and set it as needed.
*/
Startactivity (mintent );
}

Try {
Thread. Sleep (100 );
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

}
}
};

Taskwatcher.exe cute (null );

}

If any, please correct me.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.