首先简单说一下实现的思路,在MainActivity中启动一个服务,服务中注册锁屏广播监听,监听到锁屏状态改变启动LockScreenActivity作为锁屏页面,实现如下:
首先在MainActivity的布局中写一个SwitchCompat用于开关锁屏。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><RelativeLayoutandroid:id="@+id/ll_lockscreen_setting_switch_layout"android:layout_width="match_parent"android:layout_height="74dp"android:background="#ffffff"android:orientation="horizontal"android:paddingLeft="10dp"android:paddingRight="10dp"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="锁屏阅读"android:textColor="#333333"android:textSize="17sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="开启后,锁屏切换成自己的"android:textColor="#666666"android:textSize="13sp" /></LinearLayout><android.support.v7.widget.SwitchCompatandroid:id="@+id/swc_lockscreen_open"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_gravity="center_vertical"/></RelativeLayout></RelativeLayout>

MainActivity的逻辑如下所示,这里没有做状态保持,实际开发可以简单的通过SP实现。

public class MainActivity extends AppCompatActivity {private SwitchCompat mOpenLockScreenSWC;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {mOpenLockScreenSWC = (SwitchCompat) findViewById(R.id.swc_lockscreen_open);mOpenLockScreenSWC.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked) {//开启锁屏服务startLockScreen();} else {//关闭锁屏服务stopLockScreen();}}});}/*** 开启锁屏服务*/public void startLockScreen(){Intent intent = new Intent(MainActivity.this,LockScreenService.class);this.startService(intent);}/*** 关闭锁屏服务*/public void stopLockScreen(){Intent intent = new Intent(MainActivity.this,LockScreenService.class);this.stopService(intent);}}

下面是服务的代码。

public class LockScreenService extends Service {private final String TAG = "LockScreenService";private LockScreenBroadcastReceiver mReceiver = null;@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();Log.i(TAG,"onCreate => ");mReceiver = new LockScreenBroadcastReceiver();IntentFilter filter = new IntentFilter();filter.addAction(Intent.ACTION_SCREEN_OFF);filter.addAction(Intent.ACTION_SCREEN_ON);filter.addAction(Intent.ACTION_USER_PRESENT);registerReceiver(mReceiver,filter);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i(TAG,"onStartCommand => 启动了服务");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {super.onDestroy();Log.i(TAG,"onDestroy => 销毁了服务");if(mReceiver != null){unregisterReceiver(mReceiver);}}
}

广播接收的代码。

public class LockScreenBroadcastReceiver extends BroadcastReceiver {private final String TAG = "LockScreenBR";@Overridepublic void onReceive(final Context context, Intent intent) {if (intent != null) {String action = intent.getAction();Log.i(TAG, "LockScreenBroadcastReceiver => receiver => [action : " + action + "]");if (action != null && action.equals(Intent.ACTION_SCREEN_ON)) {//开启try {Intent startIntent = new Intent(context, LockScreenActivity.class);startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);context.startActivity(startIntent);} catch (Exception e) {e.printStackTrace();}}}}
}

锁屏页面的布局简单的写一个。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#000000"tools:context=".LockScreenActivity"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:src="@mipmap/ic_launcher"/></RelativeLayout>

锁屏页面要做一些特殊的处理,比如设置锁屏显示,设置隐藏虚拟按键等,(如果需要显示时间日期就监听系统时间显示,当然还有手势解锁,滑动解锁)我这里只做简单展示。

public class LockScreenActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//设置当前Activity的锁屏显示KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);KeyguardManager.KeyguardLock keyguardLock = km.newKeyguardLock("1");keyguardLock.disableKeyguard();this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);setContentView(R.layout.activity_lock_screen);//隐藏虚拟按键hideBottomButton();}/*** 隐藏虚拟按键*/private void hideBottomButton() {try{//隐藏虚拟按键if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {View v = getWindow().getDecorView();v.setSystemUiVisibility(View.GONE);} else if (Build.VERSION.SDK_INT >= 19) {View decorView = getWindow().getDecorView();int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY| View.SYSTEM_UI_FLAG_FULLSCREEN;decorView.setSystemUiVisibility(uiOptions);}}catch (Exception e){e.printStackTrace();}}
}

AndroidManifest.xml如下。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"package="com.github.gjp.lockscreendemo"><uses-permission android:name="android.permission.DISABLE_KEYGUARD" /><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"tools:ignore="GoogleAppIndexingWarning"><meta-dataandroid:name="android.max_aspect"android:value="2.2" /><service android:name=".LockScreenService" android:exported="false"/><activity android:name=".LockScreenActivity"android:configChanges="orientation|keyboardHidden|screenSize"android:launchMode="singleInstance"android:taskAffinity="com.demo.lockscreen"android:theme="@style/lockScreenActivityTheme"></activity><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

最后安装之后需要去设置页面给当前应用锁屏显示的权限,如下图所示。

以上的代码只是一个实现思路,如果要开发商用还有很多地方需要完善。

android锁屏显示相关推荐

  1. android锁屏显示应用程序,今日应用:微软又给 Android 做了一款锁屏应用

    微软又做了一款 Android 锁屏应用,质量还不错.如果你已经设置了锁屏,Picturesque可能让你再解锁一次你真的需要在锁屏就处理这么多任务吗? 微软又来给 Android 提供应用了,他们昨 ...

  2. android 锁屏显示音乐播放器,Android锁屏界面控制音乐播放

    目前,在锁屏界面控制音乐播放有两种常用方式. 第一种方式:原生Android系统及自带音乐播放器. 锁屏界面端: 原生Android中,锁屏界面相关的UI由KeyguardHostView提供,Key ...

  3. Android锁屏状态下信息的显示

    Android窗口的显示很简单,但很多人不知道锁屏状态下窗口的显示,今天就来展示一下. 先上QQ的效果图 1:显示 就一句代码,在需要显示的Activity中onCreate方法中加入 this.ge ...

  4. Android 8.1 修改锁屏显示时间

    平台 RK3399 + Android 8.1 需求 加长锁屏显示时间, 延长用户解锁时间 补丁 |-- frameworks/base/core/res/res/values/config.xml ...

  5. [Android] Android 锁屏实现与总结 (一)

    实现锁屏的方式有多种(锁屏应用.悬浮窗.普通Activity伪造锁屏等等).但国内比较主流并且被广泛应用的Activity伪造锁屏方式. 实例演示图片如下: 系列文章链接如下: [Android] A ...

  6. 网易云音乐等三方app如何在锁屏显示

    这里我本来准备反编译一下网易云音乐,但是发现github上有一个仿照网易云音乐的开源项目,参考自 https://github.com/aa112901/remusic ,其他的应用在锁屏上显示的原理 ...

  7. android锁屏应用系统排行榜,重塑安卓手机的20大锁屏应用程序

    1. AcDisplay 它是一个简单的设计android锁屏应用程序,以简约的方式处理通知.您可以直接从锁定屏幕访问应用程序.它具有使用传感器唤醒设备的活动模式. 兼容性 - Android 4.1 ...

  8. jQuery仿Android锁屏图案应用插件

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Android锁屏实现与总结

    Android锁屏实现与总结 Android锁屏实现与总结(网易云阅读) 一.自定义锁屏基本原理 二.重要步骤 1.广播注册 2.Activity设置 3.按键的屏蔽 4.滑屏解锁 5.Event b ...

  10. android 锁屏通知

    最近有个需求,说要弄个锁屏通知,通知倒是做过很多了,锁屏通知还真没弄过,经过一番研究,这里做个记录,方便搬砖. 话不多少,直接上效果图: 直接上代码: 安卓系统7以及以下: Notification. ...

最新文章

  1. Python组合数据类型之集合类型
  2. 频频曝出程序员被抓,我们该如何避免面向监狱编程?
  3. 211高校神级硕士论文刷屏!75行字错了20行!学校回应:导师停招
  4. SQL SERVER数据库修改是否区分大小写
  5. 期望文件系统格式在“1”到“4”之间;发现格式“6”
  6. 用栈解决四则运算问题
  7. ddd领域驱动设计_领域驱动设计(DDD)理论启示
  8. vSAN其实很简单-运维工程师眼里的vSAN
  9. UVA10194 Football (aka Soccer)【排序】
  10. cmd bat 相对命令
  11. 2022爱分析· 汽车行业数字化厂商全景报告
  12. 如何用计算机计算胸围,胸围尺码换算(罩杯自动计算器)
  13. c#轻量级高并发物联网服务器接收程序源码
  14. python弹性碰撞次数圆周率_关于“用理想弹性碰撞能用来计算π”视频的小讨论...
  15. sai钢笔图层编辑路径工具如何取消选择
  16. 2012移动互联网之人在囧途
  17. ps钢笔工具的详细讲解
  18. 如何查找北京驾照体检医院
  19. html语言字体如何变大,怎么把网页的字变大_怎么让html字体变大?
  20. 世界 5G 通信频段和运行模式

热门文章

  1. bitcoin简析一
  2. Winxp不幸中毒以及手杀过程
  3. 创维数字--驱动开发岗位面试总结
  4. ecshop 在确认收货时新增加商品评价并送消费积分功能
  5. linux无法识别NIC,linux – 为什么ethtool没有向我显示NIC的所有属性?
  6. PHP实现身份证认证和银行卡认证
  7. 【115】StrokeIt相关操作
  8. uni.navigateTo页面跳转时传对象参数
  9. react devtools插件报错处理
  10. [译] 海量视频时代下的内容发现之旅