Android implementation of music player lock screen page

This example shares the specific code of the lock screen page of Android music player for your reference. The specific contents are as follows

Let's take a look at the renderings first

Next, let's talk about the implementation logic. The main idea is to create an activity to cover the lock screen page.

1、 We create a new lockactivity. Since it is one of the four components, it is essential to register it in androidmanifest.xml:

<activity
  android:name=".LockActivity"
  android:excludeFromRecents="true"
  android:exported="false"
  android:launchMode="singleInstance"
  android:noHistory="true"
  android:screenOrientation="portrait"
  android:taskAffinity="com.ztk.lock"
  android:theme="@style/LockScreenTheme"/>

Note here that for the startup mode of lockactivity, we use singleinstance to store it separately in an activity task.

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

Android: nohistory = "true" indicates that the activity does not leave a history trace in the task. The style file is as follows:

<style name="LockScreenTheme" parent="AppTheme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:windowAnimationStyle">@null</item>
    <item name="android:windowContentOverlay">@null</item>
 </style> 

2、 Add a flag in the oncreate method of lockactivity so that it can be displayed on the lock screen page:

@Override
protected 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开始需要把颜色设置透明,否则导航栏会呈现系统认的浅灰色
      Window window = activity.getWindow();
      View decorView = window.getDecorView();
      //两个 flag 要结合使用,表示让应用的主体内容占用系统状态栏的空间
      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);
  }
 }
}

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

@Override
public void onBackPressed() {}

4、 Slide to the right to destroy the page. Here we need to use the knowledge of touch feedback to customize a view of slidingfinishlayout, inherit relativelayout and reference it in the layout file of lockactivity. Here, override the ontouchevent method:

@Override
public 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:      i
      sSliding = false;
      if (mParentView.getScrollX() <= -viewWidth / 4) {
      isFinish = true;
      scrollRight();
      } else {
         scrollOrigin();
         isFinish = false;
       }
      break;
    default:
    break;
  }
  return true;
}

Only the main code is posted here. Please see the demo for the detailed code. There will be the demo address at the end of the article.

5、 The realization of sliding unlocking text below is realized by using color gradient and matrix translation:

public class HintTextView extends AppCompatTextView {
  private Paint paint;
  private int mWidth;
  private LinearGradient gradient;
  private Matrix matrix;
  /**
   * 渐变的速度
   */
  private int deltaX; 

  public HintTextView(Context context) {
    super(context,null);
  } 

  public HintTextView(Context context,AttributeSet attrs) {
    super(context,attrs);
  }  

  {
  paint = getPaint();
  } 

  @Override
  protected void onSizeChanged(int w,int h,int oldw,int oldh) {
    super.onSizeChanged(w,h,oldw,oldh);
    if(mWidth == 0 ){
      mWidth = getMeasuredWidth();
      //颜色渐变器
      gradient = new LinearGradient(0,mWidth,new int[]{Color.GRAY,Color.WHITE,Color.GRAY},new float[]{0.3f,0.5f,1.0f},Shader.TileMode.CLAMP);
      paint.setShader(gradient);
      matrix = new Matrix();
      }
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if(matrix !=null){
      deltaX += mWidth / 8;
      if(deltaX > 2 * mWidth){
        deltaX = -mWidth;
       }
    }
    //通过矩阵的平移实现
    matrix.setTranslate(deltaX,0);
    gradient.setLocalMatrix(matrix);
    postInvalidateDelayed(100);
  }
}

6、 Finally, we first create a new service to receive the lock screen key event logic, so that after it is started, it can respond to the lock screen event on any page, and let lockactivity appear on the lock screen page.

1. Register the service in androidmanifest.xml:

<service
  android:name=".service.PlayService"
  android:process=":main" />

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

ScreenBroadcastReceiver screenBroadcastReceiver;
@Nullable
@Override
public IBinder onBind(Intent intent) {
  return null;
}
@Override
public 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 {
  @Override
  public 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 completed. It should be noted that some mobile phones such as Xiaomi, vivo and Meizu have the right to lock screen display. It is closed by default and needs to be opened manually.

Demo address: lockdemo

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>