Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the locking screen Page of Music player by Android

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "Android how to achieve music player lock screen page", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to achieve music player lock screen page" this article.

The details are as follows

The main idea to implement the logic is to create a new activity and overlay it on the lock screen page.

First, we create a new LockActivty. Since it is one of the four major components, it is necessary to register in the AndroidManifest.xml:

Note here that for the startup mode of LockActivty, we use singleInstance to keep it in a separate activity task.

Android:exported= "false" tag, which is used to indicate that it cannot be called or interacted with by other application components.

Android:noHistory= "true", which means that the Activity does not leave a historical trace in the task. The style file is as follows:

True @ android:color/transparent @ null false @ null @ null

Add a flag to the onCreate method of LockActivty so that it can be displayed on the lock screen page:

@ Overrideprotected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); getWindow () .addFlags (WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); fullScreen (this); setContentView (R.layout.activity_lock);}

The full-screen code fullScreen (this) is also added here:

Public static void fullScreen (Activity activity) {if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.KITKAT) {if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.LOLLIPOP) {/ / 5.x needs to be set transparently, otherwise the navigation bar will show the system default light gray Window window = activity.getWindow (); View decorView = window.getDecorView () / / two flag should be used together, which means that the main content of the application is allowed to occupy the space of the system status bar int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; decorView.setSystemUiVisibility (option); window.addFlags (WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor (Color.TRANSPARENT);} else {Window window = activity.getWindow () WindowManager.LayoutParams attributes = window.getAttributes (); int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; attributes.flags | = flagTranslucentStatus; window.setAttributes (attributes);}

Override the physical return key so that it cannot respond to the return key.

@ Overridepublic void onBackPressed () {}

4. Swipe to the right to destroy the page. Here we need to use the knowledge of touch feedback to customize a view of SlidingFinishLayout that inherits the RelativeLayout reference in the layout file of LockActivity. Here we override the onTouchEvent method:

Overridepublic boolean onTouchEvent (MotionEvent event) {switch (event.getActionMasked ()) {case MotionEvent.ACTION_DOWN: downX = tempX = (int) event.getRawX (); downY = (int) event.getRawY (); break; case MotionEvent.ACTION_MOVE: int moveX = (int) event.getRawX (); int deltaX = tempX-moveX; tempX = moveX If (Math.abs (moveX-downX) > mTouchSlop & & Math.abs ((int) event.getRawY ()-downY)

< mTouchSlop) { isSliding = true; } if (moveX - downX >

= 0 & & isSliding) {mParentView.scrollBy (deltaX, 0);} break; case MotionEvent.ACTION_UP: isSliding = false; if (mParentView.getScrollX () 2 * mWidth) {deltaX =-mWidth;}} / / matrix.setTranslate (deltaX, 0) through the translation of the matrix; gradient.setLocalMatrix (matrix); postInvalidateDelayed (100);}}

Finally, we first create a new service to receive the lock screen key event logic, so that after it starts, any page can respond to the lock screen event to make LockActivity appear on the lock screen page.

1. Register service in AndroidManifest.xml:

2. Register the broadcast receiving lock screen event in service, and jump to the lock screen page:

ScreenBroadcastReceiver screenBroadcastReceiver;@Nullable@Overridepublic IBinder onBind (Intent intent) {return null;} @ Overridepublic void onCreate () {super.onCreate (); screenBroadcastReceiver = new ScreenBroadcastReceiver (); final IntentFilter filter = new IntentFilter (); filter.addAction (Intent.ACTION_SCREEN_OFF); registerReceiver (screenBroadcastReceiver, filter);} public class ScreenBroadcastReceiver extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {handleCommandIntent (intent);}} private void handleCommandIntent (Intent intent) {final String action = intent.getAction () If (Intent.ACTION_SCREEN_OFF.equals (action)) {Intent lockScreen = new Intent (this, LockActivity.class); lockScreen.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK); startActivity (lockScreen);} @ Override public void onDestroy () {super.onDestroy (); unregisterReceiver (screenBroadcastReceiver);}

In this way, the implementation of the lock screen page is probably complete. It is important to note that some phones such as Xiaomi, vivo, Meizu and other phones have the permission to display the lock screen, which is turned off by default and needs to be opened manually.

The above is all the content of the article "how to realize the lock screen page of music player by Android". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report