一个最简单的自定义锁屏应用实现

news/2024/10/17 16:26:45/

实现思路

  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();}
}

http://www.ppmy.cn/news/462076.html

相关文章

ubuntu锁屏

用过ubuntu的人都知道&#xff0c;锁屏的快捷键就是&#xff1a;CtrlAltL 但装了gnome桌面之后不知道为啥&#xff0c;就是用不了&#xff0c;于是找到了锁屏的指令&#xff1a;gnome-screensaver-command -l 有了指令&#xff0c;那可就方便啦&#xff0c;自己添加一个呗 S…

锁定计算机 背景图片,win7系统电脑更换锁屏壁纸的方法

当win7系统电脑在一段时间不动的话就进入锁屏状态&#xff0c;然而很多用户觉得默认的锁屏壁纸不好看&#xff0c;就想要更换自己喜欢的锁屏壁纸&#xff0c;那么win7怎么更换锁屏壁纸呢&#xff1f;下面给大家讲解一下win7系统电脑更换锁屏壁纸的方法。 1、同时按下窗口键winR…

锁屏简介

锁屏代码位置 android4.4版本之前锁屏代码位于: frameworks\base\policy\src\com\android\internal\policy\impl\keyguard\ android4.4版本上锁屏代码位于: frameworks\base\packages\Keyguard\src\com\android\keyguard\ 并有单独的apk: /system/priv-app/Keyguard.apk …

锁屏效果

项目用到的一个锁屏效果&#xff0c;搞了两天&#xff0c;总结一下&#xff1a; 效果为 跟着手指的滑动部分逐渐透明&#xff0c;时间布局逐渐下移直至消失 代码如下&#xff1a;参考了http://blog.163.com/www_iloveyou_com/blog/static/21165837220154280392798/ import …

锁屏界面

锁屏的代码在 /frameworks/polices/bases/phone/com/android/internal/policy/impl/LockScreen.java 可以设置锁屏功能开关的代码位于&#xff1a; frameworks/policies/base/phone/com/android/internal/policy/impl/LockPatternKeyguardView /738 packages\apps\Settings\A…

电脑 ctf占用过高

这样会导致键盘输入反应慢 方法&#xff1a; 下载个别的输入法&#xff0c;然后把微软自带的输入法全部删了。

CTF占用CPU过高是创建bat文件贴下双击无需重启

del %USERPROFILE%\AppData\Roaming\Microsoft\InputMethod\Chs*.tmp

“程序已停止工作”问题的解决方法

“程序已停止工作”问题的解决方法 参考文章&#xff1a; &#xff08;1&#xff09;“程序已停止工作”问题的解决方法 &#xff08;2&#xff09;https://www.cnblogs.com/447367342/p/4375752.html 备忘一下。