A custom lock screen example analysis of Android programming _android

Source: Internet
Author: User
Tags stub

This example describes the custom lock screen for Android programming. Share to everyone for your reference, specific as follows:

Spent half a day studying the custom lock screen, found that the implementation is not very mysterious. But some places are worth noting.

First of all, the process, the lock screen is usually turned on when the screen is enabled, open the screen to show in front of us, so we know the timing of the lock screen, just the screen switch will emit a corresponding broadcast, so we can also like the system to capture the screen switch events.

Broadcast corresponding two action

Reference

Android.intent.action.SCREEN_ON
Android.intent.action.SCREEN_OFF

The funny thing is, I saw two more broadcasts on my me860.

Reference

Android.intent.action.batteryprofile.SCR_OFF
Android.intent.action.batteryprofile.SCR_ON

This may be Moto's own definition of the broadcast, see you need.

If you only intercept the broadcast, how does the screen lock interface replace the system lock screen? In fact, we do not replace the system lock screen, but our own lock screen opened the system lock screen, so as to achieve a replacement effect.

Here is still to note that the above action cannot be registered in Androidmanifest.xml and cannot be triggered. So here I use the method is to put in a service dynamic registration, interception broadcast normal.

This also incidentally mention the service kill function, because the service if the process does not exist, then the screen switch broadcast is in any case can not receive.

If the system is recycled service, the system will be in a period of time, sufficient resources to start up again, but we do not want their own service to the bench, so to deal with the system shutdown service we take the following methods.

In the service lifecycle:

Intent Startintent=null;
@Override public
 void OnStart (Intent Intent, int startid) {
  startintent=intent;
  Intentfilter filter=new intentfilter ();
  Filter.addaction (intent.action_screen_on);
  Filter.addaction (Intent.action_screen_off);
  Receiver=new relativebroadcastreceiver ();
  Registerreceiver (receiver, filter);
  SYSTEM.OUT.PRINTLN ("Service OnStart and Action is" +intent.getaction ());
  SYSTEM.OUT.PRINTLN ("Service OnStart and Startid is" +startid);
 @Override public
 void OnDestroy () {
  //TODO auto-generated the method stub
  System.out.println ("service OnDestroy ");
  Unregisterreceiver (receiver);
  if (startintent!=null) {
   System.out.println ("Serviceintent not NULL");
   StartService (startintent);
  }

We reserve the intent to open the service, and when we enter the OnDestroy cycle, we start again, and the system sees that you have to promise your resident memory.

However, if some memory management software killed the program process, the above method is useless, we can still intercept some key broadcasts to start their own service, similar to 91 of assistants and so is the case. You can listen for WiFi connections, changes in battery power, and so on broadcast to start your service.

Make sure your service is resident, and it's time for our own broadcast receivers to play a role.

@Override public
 void OnReceive (context context, Intent Intent) {
  //TODO auto-generated method stub
  String a Ction=intent.getaction ();
  SYSTEM.OUT.PRINTLN ("Action is" +action);
  Intent lockintent=new Intent (context,mylockscreen.class);
  Lockintent.addflags (intent.flag_activity_new_task);
  Context.startactivity (lockintent);
}

Need to add lockintent.addflags (Intent.flag_activity_new_task), the system will not add the error, but added later also have problems, this will cause multiple exits to exit the custom lock screen interface.
In fact, you can customize a stack to manage these activity, there are other ways of students please prompt me to correct.

After starting an activity we found the original lock screen, which is also mentioned earlier, our lock screen needs to open the system lock screen.
Open the System lock screen:

Super.oncreate (savedinstancestate);
GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
Setcontentview (R.layout.main);

With the addition of these two flag, the system lock screen is replaced with our own lock screen interface.

The lock screen replacement function is complete. In fact, can also modify the system lock screen, but the design to the frame layer of the modification, the promotion is also very troublesome, so skim not to say.

I hope this article will help you with the Android program.

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.