Android Software development Imitation new QQ lock screen under the window function _android

Source: Internet
Author: User

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

The following is the implementation process.

1. Use activity, not view

QQ window at the beginning I thought it is suspended view, with WindowManager to add, but in any case is not shown, and later in a friend prompted to change activity to achieve, in the lock screen state can play the window.

setting of 2.Activity

The activity requires the following settings before the window can be played in the lock screen state.

The first is the OnCreate method, which requires the addition of 4 flags, as follows:

protected void OnCreate (Bundle savedinstancestate) { 
super.oncreate (savedinstancestate); 
Final Window win = GetWindow (); 
Win.addflags (WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
Own code 
}

The four sign bits as the name implies, is the lock screen state display, unlock, keep the screen long bright, open the screen. So when the activity starts, it unlocks and displays the screen.

Then, in the Androidmanifest.xml file, the declaration of the activity requires the following attributes to be added:

<activity android:name= ". Alarm. Alarmhandleractivity " 
android:launchmode=" singleinstance " 
android:excludefromrecents=" true " 
Android:taskaffinity= "" 
android:theme= "@android: Style/theme.wallpaper.notitlebar"/>

For layout files, the view is centered and the background is transparent. The background of the desktop is displayed because the background is set to the background of the wallpaper. If the background is set to the default white, it causes the window to be white behind it and looks ugly. If the background is set to transparent, then the window will display after the unlocked interface (even if there is a lock screen password, it will show the interface after the unlock), the same impact on the visual effects.

3. Start the Lock screen window in the broadcast

We set the lock screen only to play the window, not under the lock screen is not suitable for the pop-up window (you can try it, the effect will be very strange). It is generally registered to register a broadcast receiver, after receiving the specified broadcast to determine whether the need for the window, so in the Broadcastreceiver receive code to determine whether it is a lock screen state:

@Override public 
void OnReceive (context context, Intent Intent) { 
log.d (Log_tag, Intent.getaction ()); 
Keyguardmanager km = (keyguardmanager) context.getsystemservice (context.keyguard_service); 
if (Km.inkeyguardrestrictedinputmode ()) { 
Intent alarmintent = new Intent (context, alarmactivity.class); 
Alarmintent.addflags (intent.flag_activity_new_task); 
Context.startactivity (alarmintent); 
} 

Here is the Keyguardmanager class used to manage the lock screen, after 4.1 The API for this class adds a iskeyguardlocked () method to determine whether to lock the screen, But before 4.1, we can only use the Inkeyguardrestrictedinputmode () method, if true, that is the lock screen state. It should be noted that the context in which the activity is initiated in the broadcast may not be an activity object, so you need to add a new_task flag, otherwise you may be able to make an error at startup.

4. Update the window information

If the window activity itself does not actively update the information, when there is new information need to update the activity interface, because on the above we set the SingleInstance boot mode, so we need to overwrite onnewintent (Intent Intent) method, This way, when the activity is started again, the new intent is passed in to the method.

5. Light up the screen again

If the activity does not exit but is manually pressed, the screen will not be aroused when the front broadcast receiver launches it again, so we need to add the Wake screen code to the activity, which uses a power lock. Can be added in Onnewintent (Intent Intent) as it will be invoked. can also be added in other appropriate lifecycle methods. Add the following code:

PowerManager pm = (powermanager) this.getsystemservice (context.power_service); 
if (!pm.isscreenon ()) { 
Powermanager.wakelock wl = pm.newwakelock (Powermanager.acquire_causes_wakeup | 
Powermanager.screen_bright_wake_lock, "BRIGHT"); 
Wl.acquire (); 
Wl.release (); 
}

6. Some authority

Here are some of the permissions that are required in the implementation process, because I am extracted from the project code, inevitably add or omit, developers pay attention to:

<uses-permission android:name= "Android.permission.DISABLE_KEYGUARD"/> 
<uses-permission android:name = "Android.permission.WAKE_LOCK"/>

The first one is needed to unlock the screen, and the second one is required for a power lock.

The above is a small series to introduce the Android program to develop imitation of the new version of QQ lock screen under the window function, I hope to help you!

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.