实现思路

  1. 该锁屏的实现思路是在锁屏显示的时候,屏蔽掉系统原来的屏保,显示自己的屏保程序
  2. 锁屏调用的时机有两个,一个是在手机开机时(可以监听开机广播),第二个是在屏幕灭屏/亮屏切换时候(可以监听屏幕灭/亮的广播)。

实现

SystemEventReceiver

接受系统开机广播的Receiver,并启动Service

package com.lockscreen;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;/*** Created by jun on 17-4-24.*/
public class SystemEventReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {context.startService(new Intent(context, LockScreenService.class));}}
}

MainActivity

锁屏显示的界面

package com.lockscreen;import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.activity_main).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {MainActivity.this.startService(new Intent(MainActivity.this, LockScreenService.class));}});}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.lockscreen"><!--解除键盘锁 所需要权限--><uses-permission android:name="android.permission.DISABLE_KEYGUARD" /><!--开机广播--><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid:name=".MainActivity"android:excludeFromRecents="true"><!--使应用不再最近应用列表中显示--><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><service android:name=".LockScreenService"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" /></intent-filter></service><!--处理系统广播--><receiver android:name=".SystemEventReceiver"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" /></intent-filter></receiver></application>
</manifest>

LockScreenService

处理锁屏的Service

处理锁屏的Servicepackage com.lockscreen;import android.app.KeyguardManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;/*** Created by jun on 17-4-24.*/public class LockScreenService extends Service {private final String TAG = this.getClass().getName();private KeyguardManager km;private KeyguardManager.KeyguardLock kk;private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {Log.d(TAG, "onReceive: start");KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);KeyguardManager.KeyguardLock kk = km.newKeyguardLock("");kk.disableKeyguard();Intent service = new Intent();service.setClass(context, MainActivity.class);service.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(service);}};@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);kk = km.newKeyguardLock("");kk.disableKeyguard();}@Overridepublic void onStart(Intent intent, int startId) {super.onStart(intent, startId);Log.d(TAG, "onStart: ");//通知状态条notifySpinnerBar();//亮屏/灭屏广播只能动态监听IntentFilter iFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);iFilter.setPriority(1000);this.registerReceiver(broadcastReceiver, iFilter);}//将本服务显示到SpinnerBar上private void notifySpinnerBar() {Notification notify = new Notification(R.mipmap.ic_launcher, null, 0);//将此通知放到通知栏的Ongoing组中,也就是正在运行的组中notify.flags |= Notification.FLAG_ONGOING_EVENT;
//        notify.flags |= Notification.FLAG_AUTO_CANCEL;
//        notify.flags |= Notification.FLAG_FOREGROUND_SERVICE;
//        notify.flags |= Notification.FLAG_INSISTENT;
//        notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
//        notify.flags |= Notification.FLAG_SHOW_LIGHTS;
//        notify.flags |= Notification.FLAG_NO_CLEAR;//定义Notification出现声音notify.defaults |= Notification.DEFAULT_SOUND;//设置如何震动notify.defaults |= Notification.DEFAULT_LIGHTS;//设置lec灯颜色notify.ledARGB = Color.BLUE;notify.ledOnMS = 5000;Intent notifyIntent = new Intent(this, MainActivity.class);PendingIntent pIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
//        notify.setLatestEventInfo(this,null,null,contentIntent);NotificationManager notifyManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);this.startForeground(0, notify);//当id=0时,notify将不会显示
//        this.startForeground(0, notify);}@Overridepublic void onDestroy() {kk.reenableKeyguard();super.onDestroy();}
}

一个最简单的自定义锁屏应用实现相关推荐

  1. 浅谈 Android 自定义锁屏页的发车姿势

    作者:blowUp ,原文链接:http://mp.weixin.qq.com/s?__biz=MzA3NTYzODYzMg==&mid=2653577446&idx=2&sn ...

  2. 【腾讯Bugly干货分享】浅谈Android自定义锁屏页的发车姿势

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57875330c9da73584b025873 一.为什么需要自定义锁屏页 锁屏 ...

  3. 浅谈Android自定义锁屏页的发车姿势

    一.为什么需要自定义锁屏页 锁屏作为一种黑白屏时代就存在的手机功能,至今仍发挥着巨大作用,特别是触屏时代的到来,锁屏的功用被发挥到了极致.多少人曾经在无聊的时候每隔几分钟划开锁屏再关上,孜孜不倦,其酸 ...

  4. 【腾讯Bugly干货分享】浅谈 Android 自定义锁屏页的发车姿势

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57875330c9da73584b025873 一.为什么需要自定义锁屏页 锁屏 ...

  5. Android实现自定义锁屏控制

    当在Android手机上需要实现自定义的锁屏,  往往在进入自定义的锁屏界面界面之前需要先解开屏幕锁, 以顺利的进入自定义锁屏界面 ,并能方便用户即时的做其他操作,下面用代码来实现这一功能: 1.点亮 ...

  6. Android 4.0 自定义锁屏

    在Android 4.0上做锁屏有一段时间了,期间改了很多bug,也按不同需求做了不少锁屏,其中比较满意的作品包括两个.一是,添加一个锁屏可以和原生锁屏进行切换:二是,自己写一个锁屏view去替换原生 ...

  7. html 锁屏模板,如何自定义锁屏样式

    华为手机怎么换自定义锁屏样式?只有这两个,想换个自己拍的照片 你选择了第二个,然后去设置锁屏壁纸.用你的照片当锁屏壁纸.那样就不会变换了. 怎么自定义锁屏样式,我想应用自己的图片. 手机打开主题有个混 ...

  8. android开发 自定义锁屏界面,插件锁屏桌面自定义 “安卓4.0”界面美化教程

    距离Android4.0系统的正式发布已经过去一段时间,除了最先搭载该系统的三星Galaxy Nexus.华为荣耀Android4.0商用版以及小米手机MIUI4.0等,多数用户目前仍然处于观望阶段. ...

  9. 安卓 14 可自定义锁屏时钟尺寸、颜色和透明度

    以下内容来自公众号code小生,关注每日干货及时送达 根据最新报道,预估在本月发布的安卓 14 Beta 3 更新中,将原生引入自定义锁屏时钟特性. 有媒体表示用户当前可以为锁屏和主屏幕设置不同的壁纸 ...

  10. Mac电脑的锁屏界面如何自定义锁屏消息?

    Mac锁屏消息是一种非常实用的功能,您可以设置自定义消息,使其显示在Mac的锁定屏幕上,可以放上任何你觉得有用或者有趣的消息,例如如果您放上个人的联系信息,在放错Mac或丢失Mac的时候,会非常有用, ...

最新文章

  1. Windows下搭建SVN傻瓜式教程
  2. MVC Html.AntiForgeryToken() 防止CSRF攻击
  3. 主流浏览器和内核及Web标准
  4. JSON字符串封装成Bean对象/JSON串反序列化成实体类对象/JSON字符串转换成Java对象
  5. Swift中文教程(八) 枚举类型
  6. 中级php工程师书籍,中级PHP工程师
  7. Linux 2.6中基于Sysenter的系统调用机制
  8. Kubernetes中部署SpringBoot应用
  9. arma模型matlab代码_DCC GARCH模型
  10. 力扣-1337. 矩阵中战斗力最弱的 K 行
  11. treeview 如何从多个数据表中获取数据动态生成 [提问]
  12. CentOs下编译安装nginx
  13. 记录成功通过CSP接口获取Ukey的X509数字证书过程
  14. this与bind(this)
  15. es mapping 设置
  16. 二清,是“担保支付”,还是“雁过拔毛”
  17. Python基础知识之面向对象编程介绍(二)
  18. 经典的《Rework》
  19. 软考证书的含金量有多高?
  20. 得之坦然,失之淡然顺其自然,争其必然,真是太经典

热门文章

  1. android手机和包支付,中国移动和包支付客户端下载-和包支付appv9.7.16 安卓版-手机腾牛网...
  2. 宫廷秘方,给大家分享一下,祝大家身体健康
  3. 安装office时总得到“安装程序包的语言不受系统支持”的提示解决方法
  4. removeNode is not defined removeNode is not a function
  5. 前端1——html笔记
  6. Zabbix 报告缺少可用的交换空间主机 “Lack of free swap space”问题解决
  7. phalapi可以依赖注入么_PhalApi:[2.11] 核心思想:DI依赖注入 让资源更可控
  8. 斯年,愿做岁月的知音
  9. WEB_BASIC---02 CSS概述、CSS语法、CSS选择器、CSS声明
  10. 高级算法日记9:图(2)