Android Development Tips-imitation of the new QQ lock screen under the pop-up window (turn)

Source: Internet
Author: User

New QQ, can be in the lock screen pop-up window display QQ message, just now in the project also need this function. After a variety of experiments and data search, and finally realized, the process is not difficult, but there are some places to pay attention to.

Here is the implementation process.

1, the use of activity, instead of the viewqq of the pop-up I thought it was suspended view, with WindowManager to add, but anyway is not displayed, and later in the friend prompt for activity to achieve, in the lock screen State can pop window. 2.Activity setting activity requires the following settings to pop the window in the lock screen state. The first is the OnCreate method, which needs to add 4 flags, as follows: [Java]View Plaincopy
  1. protected void OnCreate (Bundle savedinstancestate) {
  2. super.oncreate (savedinstancestate);
  3. final Window win = GetWindow ();
  4. Win.addflags (WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
  5. | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
  6. | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
  7. | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
  8. //own code
  9. }
The four flag bits as the name implies, are displayed in the lock screen state, unlocked, keep the screen long bright, open the screen. When the activity starts, it unlocks and displays the screen.
Then in the Androidmanifest.xml file, the declaration of the activity needs to be added to the following attributes: [HTML]View Plaincopy
  1. <activity android:name=". Alarm. Alarmhandleractivity "
  2. android:launchmode="singleinstance"
  3. android:excludefromrecents="true"
  4. android:taskaffinity=""
  5. android:theme="@android: Style/theme.wallpaper.notitlebar"/>
For the layout file, the view to be displayed is centered and the background is transparent. Because the background is already set for wallpaper background, the background of the desktop is displayed. If the background is set to the default white, it causes the pop window to be white and looks ugly. If the background is set to transparent, the unlocked interface will appear after the pop-up window (even if there is a lock screen password, which will show the unlocked interface), as well as the visual effects.
3. Start the lock screen pop-up window in the broadcast we set the lock screen to play the window, the non-lock screen is not suitable for pop-up of the windows (you can try, the effect will be very strange). The general is to register a broadcast receiver, after receiving the specified broadcast to determine whether to need to pop the window, so in the Broadcastreceiver receive code to first determine whether the lock screen state: [Java]View Plaincopy
  1. @Override
  2. public void OnReceive (context context, Intent Intent) {
  3. LOG.D (Log_tag, Intent.getaction ());
  4. Keyguardmanager km = (keyguardmanager) context.getsystemservice (Context.keyguard_service);
  5. if (Km.inkeyguardrestrictedinputmode ()) {
  6. Intent alarmintent = new Intent (context, alarmactivity.   Class);
  7. Alarmintent.addflags (Intent.flag_activity_new_task);
  8. Context.startactivity (alarmintent);
  9. }
  10. }

Here is the Keyguardmanager class, used to manage the lock screen, after 4.1, the class of API added a iskeyguardlocked () method to determine whether to lock the screen, But before 4.1, we can only use the Inkeyguardrestrictedinputmode () method, if true, is the lock screen state. It is important to note that the context in which the activity is initiated in the broadcast may not be the activity object, so you need to add a new_task flag, or you may get an error when you start. 4. Update popup information if the popup activity itself does not actively update the information, when there is new information to update the activity interface, because we set the SingleInstance boot mode above, so we need to overwrite onnewintent (Intent Intent) method so that when the activity is started again, the new intent is passed in the method. 5. On the screen again if the activity does not exit, but is manually pressed the lock screen button, when the previous broadcast receiver to start it again, the screen will not be aroused, so we need to add a wake-up screen in the activity of the code, here is a power lock. Can be added in Onnewintent (Intent Intent) as it will be called. can also be added in other appropriate life-cycle methods. Add the following code: [Java]View Plaincopy
    1. PowerManager pm = (powermanager) this.getsystemservice (Context.power_service);
    2. if (!pm.isscreenon ()) {
    3. Powermanager.wakelock wl = Pm.newwakelock (Powermanager.acquire_causes_wakeup |
    4. Powermanager.screen_bright_wake_lock, "BRIGHT");
    5. Wl.acquire ();
    6. Wl.release ();
    7. }

6. Some of the permissions below are some of the permissions required in the implementation process, as I was extracted from the project code, it is unavoidable to add or omit, the developer himself notice: [HTML]View Plaincopy
    1. <uses-permission android:name="Android.permission.DISABLE_KEYGUARD"/>
    2. <uses-permission android:name="Android.permission.WAKE_LOCK"/>

The first one is required to unlock the screen, and the second one is required for the power lock. This article original, reproduced please indicate the source: http://blog.csdn.net/maosidiaoxian/article/details/40587935

Android Development Tips-imitation of the new QQ lock screen under the pop-up window (turn)

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.