当前位置: 移动技术网 > IT编程>移动开发>Android > Android屏幕锁屏弹窗的正确姿势DEMO详解

Android屏幕锁屏弹窗的正确姿势DEMO详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

我的女上司,医疗新闻,四川公安交通信息网

在上篇文章给大家介绍了android程序开发仿新版qq锁屏下弹窗功能。今天通过本文给大家分享android锁屏弹窗的正确姿势。

最近在做一个关于屏幕锁屏悬浮窗的功能,于是在网上搜索了很多安卓屏幕锁屏的相关资料,鉴于网上的资料比较零碎,所以我在这里进行整理总结。本文将从以下两点对屏幕锁屏进行解析:

1. 如何监听系统屏幕锁屏

2. 如何在锁屏界面弹出悬浮窗

如何监听系统屏幕锁屏

经过总结,监听系统的锁屏可以通过以下两种方式:

1) 代码直接判定

2) 接收广播

1) 代码直接判定

代码判断方式,也有两种方法:

a) 通过powermanager的isscreenon方法,代码如下:

powermanager pm = (powermanager) 
context.getsystemservice(context.power_service);
//如果为true,则表示屏幕“亮”了,否则屏幕“暗”了。
boolean isscreenon = pm.isscreenon();

这里需要解释一下:

屏幕“亮”,表示有两种状态:a、未锁屏 b、目前正处于解锁状态 。这两种状态屏幕都是亮的;

屏幕“暗”,表示目前屏幕是黑的 。

b) 通过keyguardmanager的inkeyguardrestrictedinputmode方法,代码如下:

keyguardmanager mkeyguardmanager = (keyguardmanager) context.getsystemservice(context.keyguard_service);
boolean flag = mkeyguardmanager.inkeyguardrestrictedinputmode();

对flag进行一下说明,经过试验,总结为:

如果flag为true,表示有两种状态:a、屏幕是黑的 b、目前正处于锁屏状态 。

如果flag为false,表示目前未锁屏

注明:上面的两种方法,也可以通过反射机制来调用。

反射代码如下:

private static method mreflectscreenstate;
try {
mreflectscreenstate = powermanager.class.getmethod(isscreenon, new class[] {});
powermanager pm = (powermanager) context.getsystemservice(activity.power_service);
boolean isscreenon= (boolean) mreflectscreenstate.invoke(pm);
} catch (exception e) {
e.printstacktrace()
}

2) 接收广播

当安卓系统锁屏或者屏幕亮起,或是屏幕解锁的时候,系统内部都会发送相应的广播,我们只需要对广播进行监听就可以了
注册广播的伪代码如下:

private screenbroadcastreceiver mscreenreceiver;
private class screenbroadcastreceiver extends broadcastreceiver {
private string action = null;
@override
public void onreceive(context context, intent intent) {
action = intent.getaction();
if (intent.action_screen_on.equals(action)) { 
// 开屏
} else if (intent.action_screen_off.equals(action)) { 
// 锁屏
} else if (intent.action_user_present.equals(action)) { 
// 解锁
}
}
}
private void startscreenbroadcastreceiver() {
intentfilter filter = new intentfilter();
filter.addaction(intent.action_screen_on);
filter.addaction(intent.action_screen_off);
filter.addaction(intent.action_user_present);
context.registerreceiver(mscreenreceiver, filter);
}

如何在锁屏界面弹出悬浮窗

竟然知道了对于系统屏幕监听的方法,那么接下来就是要在屏幕锁屏的时候,弹出悬浮框了,这个的实现方式有两种:

1) 使用windowmanager

2) 使用activity

目前情况是,使用这两种方式在真机上都可以实现,如果网友们发现有问题,可以在博客中留言

1) 使用windowmanager

代码如下:

private void init(context mcontext) {
this.mcontext = mcontext;
mwindowmanager = (windowmanager) mcontext.getsystemservice(context.window_service);
// 更新浮动窗口位置参数 靠边
displaymetrics dm = new displaymetrics();
// 获取屏幕信息
mwindowmanager.getdefaultdisplay().getmetrics(dm);
mscreenwidth = dm.widthpixels;
mscreenheight = dm.heightpixels;
this.mwmparams = new windowmanager.layoutparams();
// 设置window type
if (build.version.sdk_int >= build.version_codes.kitkat) {
mwmparams.type = windowmanager.layoutparams.type_toast;
} else {
mwmparams.type = windowmanager.layoutparams.type_phone;
}
// 设置图片格式,效果为背景透明
mwmparams.format = pixelformat.rgba_8888;
// 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
mwmparams.flags = windowmanager.layoutparams.flag_not_focusable;
// 调整悬浮窗显示的停靠位置为左侧置�?
mwmparams.gravity = gravity.left | gravity.top;
mscreenheight = mwindowmanager.getdefaultdisplay().getheight();
// 以屏幕左上角为原点,设置x、y初始值,相对于gravity
mwmparams.x = 0;
mwmparams.y = mscreenheight / 2;
// 设置悬浮窗口长宽数据
mwmparams.width = layoutparams.wrap_content;
mwmparams.height = layoutparams.wrap_content;
addview(createview(mcontext));
mwindowmanager.addview(this, mwmparams);
mtimer = new timer();
hide();
}

windowmanager的主要配置就是上面的那些代码,这里需要说明一下,type的类型有如下值:

应用程序窗口。
public static final int first_application_window = 1; 
所有程序窗口的“基地”窗口,其他应用程序窗口都显示在它上面。 
public static final int type_base_application =1;
普通应用功能程序窗口。token必须设置为activity的token,以指出该窗口属谁。
public static final int type_application = 2;
用于应用程序启动时所显示的窗口。应用本身不要使用这种类型。
它用于让系统显示些信息,直到应用程序可以开启自己的窗口。 
public static final int type_application_starting = 3; 
应用程序窗口结束。
public static final int last_application_window = 99;
子窗口。子窗口的z序和坐标空间都依赖于他们的宿主窗口。
public static final int first_sub_window = 1000;
面板窗口,显示于宿主窗口上层。
public static final int type_application_panel = first_sub_window;
媒体窗口,例如视频。显示于宿主窗口下层。
public static final int type_application_media = first_sub_window+1;
应用程序窗口的子面板。显示于所有面板窗口的上层。(gui的一般规律,越“子”越靠上)
public static final int type_application_sub_panel = first_sub_window +2;
对话框。类似于面板窗口,绘制类似于顶层窗口,而不是宿主的子窗口。
public static final int type_application_attached_dialog = first_sub_window +3;
媒体信息。显示在媒体层和程序窗口之间,需要实现透明(半透明)效果。(例如显示字幕)
public static final int type_application_media_overlay = first_sub_window +4;
子窗口结束。( end of types of sub-windows )
public static final int last_sub_window = 1999;
系统窗口。非应用程序创建。
public static final int first_system_window = 2000;
状态栏。只能有一个状态栏;它位于屏幕顶端,其他窗口都位于它下方。
public static final int type_status_bar = first_system_window;
搜索栏。只能有一个搜索栏;它位于屏幕上方。
public static final int type_search_bar = first_system_window+1;
电话窗口。它用于电话交互(特别是呼入)。它置于所有应用程序之上,状态栏之下。
public static final int type_phone = first_system_window+2;
系统提示。它总是出现在应用程序窗口之上。
public static final int type_system_alert = first_system_window +3;
锁屏窗口。
public static final int type_keyguard = first_system_window +4;
信息窗口。用于显示toast。
public static final int type_toast = first_system_window +5;
系统顶层窗口。显示在其他一切内容之上。此窗口不能获得输入焦点,否则影响锁屏。
public static final int type_system_overlay = first_system_window +6;
电话优先,当锁屏时显示。此窗口不能获得输入焦点,否则影响锁屏。
public static final int type_priority_phone = first_system_window +7;
系统对话框。(例如音量调节框)。
public static final int type_system_dialog = first_system_window +8;
锁屏时显示的对话框。
public static final int type_keyguard_dialog = first_system_window +9;
系统内部错误提示,显示于所有内容之上。
public static final int type_system_error = first_system_window +10;
内部输入法窗口,显示于普通ui之上。应用程序可重新布局以免被此窗口覆盖。
public static final int type_input_method = first_system_window +11;
内部输入法对话框,显示于当前输入法窗口之上。
public static final int type_input_method_dialog= first_system_window +12;
墙纸窗口。
public static final int type_wallpaper = first_system_window +13;
状态栏的滑动面板。
public static final int type_status_bar_panel = first_system_window +14;
系统窗口结束。
public static final int last_system_window = 2999;

如果想让悬浮窗在所以锁屏之上,使用type_system_error,因为它显示在所有内容之上。

2) 使用activity

activity的设置

activity需要进行以下设置,才可以在锁屏状态下弹窗。
首先是oncreate方法,需要添加4个标志,如下:

protected void oncreate(bundle savedinstancestate) { 
super.oncreate(savedinstancestate); 
final window win = getwindow(); 
win.addflags(windowmanager.layoutparams.flag_show_when_locked 
| windowmanager.layoutparams.flag_dismiss_keyguard 
| windowmanager.layoutparams.flag_keep_screen_on 
| windowmanager.layoutparams.flag_turn_screen_on); 
// 自己的代码 
} 

四个标志位顾名思义,分别是锁屏状态下显示,解锁,保持屏幕长亮,打开屏幕。这样当activity启动的时候,它会解锁并亮屏显示。
然后在androidmanifest.xml文件当中,对该activity的声明需要加上以下属性:

<activity android:name=".alarm.alarmhandleractivity" 
android:launchmode="singleinstance" 
android:excludefromrecents="true" 
android:taskaffinity="" 
android:theme="@android:style/theme.wallpaper.notitlebar"/> 

而对于布局文件,要显示的view居中,背景透明。由于上面已经设置了背景为壁纸的背景,所以显示的是桌面的背景。如果背景设为默认的白色,则导致弹窗后面是一片白色,看起来很丑。如果背景设置为透明,则弹窗后面会显示出解锁后的界面(即使有锁屏密码,也是会显示解锁后的界面的),一样很影响视觉效果。

在广播中启动锁屏弹窗

我们设置的是锁屏下才弹窗的,非锁屏下就不适合弹出这个窗口了(你可以试一下,效果会很怪)。一般是注册一个广播接收器,在接收到指定广播之后判断是否需要弹窗,所以在broadcastreceiver的接收代码中需要先判断是否为锁屏状态下:

@override 
public void onreceive(context context, intent intent) { 
log.d(log_tag, intent.getaction()); 
keyguardmanager km = (keyguardmanager) context.getsystemservice(context.keyguard_service); 
if (km.inkeyguardrestrictedinputmode()) { 
intent alarmintent = new intent(context, alarmactivity.class); 
alarmintent.addflags(intent.flag_activity_new_task); 
context.startactivity(alarmintent); 
} 
} 

这里用到的是keyguardmanager类,用来管理锁屏的,4.1之后该类的api新增了一个iskeyguardlocked()的方法判断是否锁屏,但在4.1之前,我们只能用inkeyguardrestrictedinputmode()方法,如果为true,即为锁屏状态。需要注意的是,在广播中启动activity的context可能不是activity对象,所以需要添加new_task的标志,否则启动时可能会报错。我们就可以结合之前的系统发送广播后进行相应的悬浮窗的弹出处理。

复写onnewintent方法

再次亮起屏幕,如果该activity并未退出,但是被手动按了锁屏键,当前面的广播接收器再次去启动它的时候,屏幕并不会被唤起,所以我们需要在activity当中添加唤醒屏幕的代码,这里用的是电源锁。可以添加在onnewintent(intent intent),因为它会被调用。也可以添加在其他合适的生命周期方法。添加代码如下:

powermanager pm = (powermanager) this.getsystemservice(context.power_service); 
if (!pm.isscreenon()) { 
powermanager.wakelock wl = pm.newwakelock(powermanager.acquire_causes_wakeup | 
powermanager.screen_bright_wake_lock, "bright"); 
wl.acquire(); 
wl.release(); 
} 

最后,是添加如下权限

<uses-permission android:name="android.permission.disable_keyguard"/> 
<uses-permission android:name="android.permission.wake_lock"/> 

第一条是解锁屏幕需要的,第二条是申请电源锁需要的。

以上所说是小编给大家介绍的android屏幕锁屏弹窗的正确姿势demo详解,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网