一个简单的Android 锁屏小Demo,可以设置左滑有滑,我简单的了解一下自定义锁屏,顺便总结了一下思路顺便画个图帮助理解。
我的效果图
代码思路
锁屏分为两块,一块是UI(自定义布局)另一块这就是服务了
自定义锁屏布局
ACTION_DOWN按下:中间的ImageView会变成另一种形态
ACTION_MOVE移动: 移动到左/右边,左/右ImageView形态也要发生变化,
ACTION_UP抬起:判断滑动到左/右那边,或者回到最初位置
*服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Override
public void onCreate() {
super .onCreate();
mContext = getApplicationContext();
//管理打开的窗口程序
mWinMng = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
i = new Intent(mContext, MyService. class );
i.setAction(MyService.LOCK_ACTION);
zdLockIntent = new Intent(MyService. this , MyService. class );
//设置此状态,首先会查找是否存在和被启动的Activity具有相同的亲和性的任务栈
zdLockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
/*注册广播*/
IntentFilter mScreenOnFilter = new IntentFilter( "android.intent.action.SCREEN_ON" );
MyService. this .registerReceiver(mScreenOnReceiver, mScreenOnFilter);
/*注册广播*/
IntentFilter mScreenOffFilter = new IntentFilter( "android.intent.action.SCREEN_OFF" );
MyService. this .registerReceiver(mScreenOffReceiver, mScreenOffFilter);
}
|
这个View 就是之前自定义的View,我只说说核心的代码 稍后上Demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent!= null ){
String action = intent.getAction();
if (TextUtils.equals(action, LOCK_ACTION))
//锁屏添加view
addView();
else if (TextUtils.equals(action, UNLOCK_ACTION))
{ //解屏移除view
removeView();
}
}
return Service.START_STICKY;
}
|
当程序销毁的时候.服务要重新启动,不然这个app的锁屏,会随着app的销毁而被销毁
1
2
3
4
5
6
7
8
|
@Override
public void onDestroy(){
super .onDestroy();
MyService. this .unregisterReceiver(mScreenOnReceiver);
MyService. this .unregisterReceiver(mScreenOffReceiver);
//在此重新启动
startService( new Intent(MyService. this , MyService. class ));
}
|
当屏幕变量或者变暗的时候,要解除屏幕锁定的方法,如果不解除 那只能一直锁着了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
//屏幕变亮的广播,我们要隐藏默认的锁屏界面
private BroadcastReceiver mScreenOnReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context , Intent intent) {
Log.i(TAG, intent.getAction());
if (intent.getAction().equals( "android.intent.action.SCREEN_ON" )){
//键盘锁的服务
mKeyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = mKeyguardManager.newKeyguardLock( "zdLock 1" );
mKeyguardLock.disableKeyguard();
startService(i);
}
}
};
//屏幕变暗/变亮的广播 , 我们要调用KeyguardManager类相应方法去解除屏幕锁定
private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context , Intent intent) {
String action = intent.getAction() ;
Log.i(TAG, intent.toString());
if (action.equals( "android.intent.action.SCREEN_OFF" )
|| action.equals( "android.intent.action.SCREEN_ON" ) ){
mKeyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = mKeyguardManager.newKeyguardLock( "zdLock 1" );
mKeyguardLock.disableKeyguard();
startService(i);
}
}
};
|
权限问题
1
2
3
4
5
|
<uses-permission android:name= "android.permission.DISABLE_KEYGUARD" /><!-- 屏蔽HOME键需要的权限 -->
<uses-permission android:name= "android.permission.VIBRATE" />
<uses-permission android:name= "android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name= "android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name= "android.permission.SYSTEM_ALERT_WINDOW" />
|
这个锁屏Demo涉及到的东西有点多,我写的这个博客掐不住重点,写的繁琐反而浪费大家时间,什么也不说了全在代码中↓
源码地址:锁屏
补充一点
用小米手机当测试机,一定要把这个Demo锁屏权限打开,手机设置中找到app,打开锁屏,不然一运行就崩掉。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/MacaoPark/article/details/73477986