QQ Music/Cool Dog Music lock screen control implementation principle

Source: Internet
Author: User

The effect I realized

Chaotic lock-screen control

Android since the 4.0 version, which is API level 14, has added the lock screen control function, the related class is RemoteControlClient , this class is marked as deprecated in API level 21, replaced by the new class MediaSession . Our Music app is the first to use the original Lock screen control API, to tell the truth this API is not good, encountered some small pits, the most deadly is different brands of mobile phones, lock screen interface long is not the same, even I have not seen the original 4.0 of the Lock screen control interface is what. Domestic handset manufacturers think their own aesthetic is very strong, design a strange lock screen control interface, MIUI more strange, MIUI 6 is on the basis of the original 4.4.4 change, unexpectedly have a period of time there is no lock screen control interface, later updated only. While native Android is 5.0, the lock screen and notification bar control merge, the entire logic is very chaotic. We decided to make a lock screen control page like QQ music/cool Dog music.

Digression: When you set a cover page for a remotecontrolclient, the parameter is a bitmap, and when this parameter is passed in, do not use the bitmap in other places, and do not hold its references, let alone call its Recycle method.

Realize the Idea lock screen application: Cannon hit mosquitoes

The first thing to think about is to do a lock screen, that is, using the Android API, do a lock screen application, and input method and other applications, but this method is very expensive. Domestic lock screen application, the first thing to do is to guide the user to set the lock screen application, the steps are quite complicated, just for a play control with a lock screen application, no user will be so patient.

Suspension window: Black Magic

Thanks to our programmer's brain hole, we have a second way of thinking: suspended Windows.
Suspension window of a more rigorous name called System Warning window, some rogue manufacturers at home and abroad, often with a floating window to play some ads, the suspension window is floating in the normal app above, so if it does not disappear, it is likely that you even normal use of mobile phones have problems.
This is a more disturbing user's things, but also have a certain security risks. MIUI Rights Management By default is the suspension window closed, and Youdao dictionary copy search Word function, is to do with the suspended window, if you do not give Youdao dictionary open this permission, copy look up the function of the word is wasted.

Ordinary activity Forged lock screen

The effect of the GIF image at the beginning of the article is to use an ordinary activity.
Domestic apps, eventually chose this path, do not know who they are copied who, the first thought of using ordinary activity to forge a lock screen developers, I can only say very creative.

Monitor lock Screen Events

Exactly what we are listening to is the screen off event, off event intent is Intent.ACTION_SCREEN_OFF , do not need any permission to listen, but must use code registration, that is, we must have a service in the background to listen to the music app, this is not a problem, The music app itself is controlled using MediaPlayer the service. You only need to register the listener in the service Intent.ACTION_SCREEN_OFF . The warden heard the incident,

@OverridepublicvoidonReceive(Context context, Intent intent) {    String action = intent.getAction();    if (action.equals(Intent.ACTION_SCREEN_OFF)) {        new Intent(PlaybackService.this, LockScreenActivity.class);        lockscreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        startActivity(lockscreen);    }}

Note that we need to add this flag in order to activate an activity in the service Intent.FLAG_ACTIVITY_NEW_TASK .

Transparent background

To make the lock screen slide to unlock, for example, like in the image, we want to move the view in addition to the gesture, but also let the activity background transparent, such as the theme set to the following.

<style name="Lockscreenbase" parent="Appbasetheme"><Item name="Android: Windowistranslucent ">true</Item> <Item name="Android: Windowbackground ">@android:color/transparent</item> <item name="Android:colorbackgroundcachehint" > @null </item> <item name="Android:windownotitle">true</item> <item name=" Android:backgrounddimenabled ">false</item> <item name=" Android:windowanimationstyle " > @null </item> <item name="Android:windowcontentoverlay"> @null </item></style>

This style actually does a lot of things, we can cut off some parts according to their needs, such as the status bar transparent, do not use titlebar and so on.

Unlock screen and display on top of lock screen

In the display of our fake lock screen, we should help the user to unlock, so that we can impersonate the lock screen, without the user "unlock" two times, but we can only ask the system to unlock the lock screen without a password, there is a password, we can not unlock the screen, then we should be covered in the lock screen, fortunately, Two flags were introduced in API Level 5, FLAG_DISMISS_KEYGUARD and FLAG_SHOW_WHEN_LOCKED
onCreateadd two flags to activity in the lock screen activity method

this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

Must be two together, otherwise the effect is not good, then tested for a long time, then read the realization of QQ music, only found two together with the effect is good, otherwise there will be some strange problems.

Hide traces and separate tasks

As a lock screen, it should be independent, that is to say, our activity should exist independently of our app, at least it looks like this. From the Android point of view, all activity in the main interface of our app should be in a task, while the lock screen activity should be in a separate task, so we need to give the lock screen activity a separate task, and whenever There is only one lock screen activity instance present.
In addition, Android has the ability to view recent tasks, we do not want the lock screen interface this independent task is displayed inside, so the lock screen activity can not be displayed in recent tasks.
Say so much, to do is very simple, only need to declare activity in the manifest to add a few attributes can

< Activity  android:excludefromrecents  = "true"  android:exported  = "false"  android:launchmode  = android:name  = android:screenorientation  = "portrait"  android:taskaffinity  =" Com.package.name.lockscreen " android:theme  = "@style/lockscreentheme"  >  </activity ;   

The above attributes android:excludeFromRecents="true" let the lock screen activity not appear in the recent task, android:launchMode="singleInstance" and ensure that the android:taskAffinity="com.package.name.lockscreen" lock screen activity has a separate task, and that this task will always have only one instance.

Do not respond to the back button

The lock screen of course does not respond to the back button, only need to rewrite the activity onBackPressed method can

@OverridepublicvoidonBackPressed() {    // do nothing}
Handling of the Home button

We can't listen to the home button, but we can change the processing when the home is in the background, such as in manifest's activity statement.

android:noHistory="true"

So if the user gets our app into the background via the home button, we'll let the activity destroy, just as we were sliding off.
If not, it is best to rewrite the activity onNewIntent to deal with the situation where the service starts the lock screen activity again because the home is in the background.

Handling a Black splash screen

Our lock screen activity after sliding "unlock", the theory is directly into the following interface, but sometimes if the following is not launcher, but an app, it is possible to flash a black screen, which is actually under the activity of the entry animation caused, Some Android versions will be very transparent when the top activity is handled somewhat strangely, we cannot guarantee that other applications do not flash the black screen, but for their own application is still possible, only in our main activity of the style to add

<item name="android:windowAnimationStyle">@null</item>

There will not be such a situation, but so the admission animation is gone, in short, how to choose to see everyone.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

QQ Music/Cool Dog Music lock screen control implementation principle

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.