下边来说一下实现逻辑,其主要思路就是新建一个activity使其覆盖在锁屏页上边。
这里注意,LockActivty的启动模式,我们使用singleInstance,使其单独存在一个activity task中。
android:exported="false"标签,这个标签是用来表示不能被其他应用程序组件调用或跟它交互。
android:noHistory="true",表示该Activity在task中不留历史痕迹。
style文件如下:
@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);
}
这里同时也加入全屏的代码 fullScreen(this):
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);
}
}
}
@Override
public void onBackPressed() {}
@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;
}
这里只贴出了主要代码,详细代码请看demo,文章末尾会有demo地址。
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, 0, mWidth, 0, 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);
}
}
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);
}
这样,锁屏页面的实现就大概完成了,有一点要注意的是像小米、vivo、等一些手机会有锁屏显示和后台弹出界面权限,默认是关闭的,需要手动打开。解决这个问题最好的方式大概是加白名单吧,哈哈。