自定义锁屏实现原理:
1.监听系统SCREEN_ON与SCREEN_OFF广播
2.屏幕变暗,启动自定义锁屏页面,使其置顶
3.屏幕点亮,屏蔽系统锁屏,自定义锁屏页面即会出现
技术点:
1.监听广播只能使用动态,所以需使用后台sevice 保持监听长期有效
注意service保活(JobService等技术)
注意开关机问题(监听开机广播,启动后台service)
2.自定义锁屏
推荐使用WindowManager.addView方式
使用activity 注意屏蔽菜单键
3.监听系统消息推送(展示QQ 、微信 等应用推送消息,如不需要可忽略)
1)4.3以上版本使用 NotificationListenerService
继承并实现onNotificationPost 、onNotifitionRemoved
需要用户授权
startActivity(new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS))
2)4.3一下版本使用 AccessibilityService
关键代码
1.屏蔽系统锁屏
传统方案:
KeyguardManager keyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE); KeyguardManager.KeyguardLock lock = keyguardManager.newKeyguardLock(""); lock.disableKeyguard();
最新方案:在自定义锁屏页面添加如下代码
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
权限添加:
android:name="android.permission.DISABLE_KEYGUARD" />
2.广播注册
public class LockerService extends Service { private ScreenOnReceiver screenOnReceiver; private ScreenOffReceiver screenOffReceiver; @Override public void onCreate() { super.onCreate(); //只能动态注册 screenOnReceiver = new ScreenOnReceiver(); IntentFilter screenOnFilter = new IntentFilter(); screenOnFilter.addAction("android.intent.action.SCREEN_ON"); registerReceiver(screenOnReceiver, screenOnFilter); screenOffReceiver = new ScreenOffReceiver(); IntentFilter screenOffFilter = new IntentFilter(); screenOffFilter.addAction("android.intent.action.SCREEN_OFF"); registerReceiver(screenOffReceiver, screenOffFilter); } @Override public int onStartCommand(Intent intent, int flags, int startId) { return Service.START_STICKY; } /** * 监听屏幕变亮的广播接收器,变亮就屏蔽系统锁屏 * * @author tongleer.com */ private class ScreenOnReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals("android.intent.action.SCREEN_ON")) { /* * 此方式已经过时,在activtiy中编写 * getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); * getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); * 两句可以代替此方式 */ // KeyguardManager keyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE); // KeyguardManager.KeyguardLock lock = keyguardManager.newKeyguardLock(""); // lock.disableKeyguard(); } } } /** * 监听屏幕变暗的广播接收器,变暗就启动应用锁屏界面activity * * @author tongleer.com */ private class ScreenOffReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals("android.intent.action.SCREEN_OFF")) { Intent i1 = new Intent(context, LockerActivity.class); i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i1); } } } @Override public void onDestroy() { super.onDestroy(); unregisterReceiver(screenOnReceiver); unregisterReceiver(screenOffReceiver); } @Override public IBinder onBind(Intent intent) { return null; } }
3.自定义锁屏
public class LockerHelper { private Context mContext; private WindowManager mWindowManager;// 窗口管理器 private View mLockView;// 锁屏视图 private WindowManager.LayoutParams wmParams; private static LockerHelper mLockerHelper; private boolean isLocked;// 是否锁定 public static synchronized LockerHelper getInstance(Context mContext) { if (mLockerHelper == null) { mLockerHelper = new LockerHelper(mContext); } return mLockerHelper; } /** * 构造方法 * @param mContext */ public LockerHelper(Context mContext) { this.mContext = mContext; init(); } /** * 初始化布局 */ private void init() { isLocked = false; // 获取WindowManager mWindowManager = (WindowManager) mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // 设置LayoutParams(全局变量)相关参数 wmParams = new WindowManager.LayoutParams(); boolean isHide= (boolean) SPUtils.get(mContext, Constant.SWITCH_NOTIFICATION,false); wmParams.type = isHide? WindowManager.LayoutParams.TYPE_SYSTEM_ERROR: WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;// 关键部分 wmParams.format = PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明 // 设置Window flag wmParams.width = WindowManager.LayoutParams.MATCH_PARENT; wmParams.height = WindowManager.LayoutParams.MATCH_PARENT; wmParams.flags = 1280; } /** * 锁屏 */ public synchronized void lock() { if (mLockView != null && !isLocked) { mWindowManager.addView(mLockView, wmParams); } isLocked = true; } /** * 解锁 */ public synchronized void unlock() { if (mWindowManager != null && isLocked) { mWindowManager.removeView(mLockView); } isLocked = false; } /** * 更新 */ public synchronized void update() { if (mLockView != null && !isLocked) { mWindowManager.updateViewLayout(mLockView, wmParams); } } /** * 设置锁屏视图 * @param v */ public synchronized void setLockView(View v) { mLockView = v; } }