Screen unlock automatic startup application (implemented via broadcast) _broadcastreceiver

Source: Internet
Author: User

To implement such a function, the program can listen for screen unlocking events and automatically start the application if a screen unlocking event occurs.

Implementation method: When the screen is unlocked, the system will broadcast the unlocked event, just register a listener in the program to listen to unlock the event broadcast.

Declare the broadcast in the Androidmanifest file, where it is more important to filter the settings and declare <action android:name= in the <intent-infilter> Android.intent.action.USER_PRESENT ">

Tip: At first you need to know screen_on this listener needs to be dynamically added if it is not reacted by statically adding to the Androidmanifest file

Then you need to be aware of how to start the application by broadcasting.

In broadcast, the method is onreceive by overloading the parent class method, which starts the activity in the method that is executed after the Broadcastreceiver receives the broadcast.

Declare intent to start an activity in the broadcast onreceive. Intent Intent = new Intent (context,activity.class);

Here you need to pay attention, at this time not through intent to start the application, why. Each application should have its own activity stack, which is used to store open activity, but because it did not open the application before, the stack does not exist, it needs to be prepared in advance, by setting intent can implement this stack creation.

Intent.setflags (Intent.flag_activity_new_task) enables the creation of stacks, the flag-activity_new_task of which is that if there is no such stack for the application, the new one, if any, is directly used, very effective.

The starting intent is also through context.startactivity (intent);

So the application will start up.

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.