Android如何实现锁屏状态下弹窗如何实现锁屏状态下弹窗
在锁屏状态下弹窗的效果我们平时并不少见,如QQ、微信和闹钟等,但是Android开发者要怎么实现这一功能
呢?下面一起来看看。
前言前言
想在锁屏上面实现弹窗,第一个想法就是利用 WindowManager 设置 Window 的 Flag,通过设置 Flag 的显示优先级来让窗口显示在
锁屏的上面。
接下来就是试验可能相关的 Window Type 属性,验证该方案是否可行。
在尝试各个 Window Type 属性之前需要明确各个 Type 所需要的权限,下面是
com.android.internal.policy.impl.PhoneWindowManager.checkAddPermission 的源码:
public int checkAddPermission(WindowManager.LayoutParams attrs) {
int type = attrs.type;
if (type < WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW
|| type > WindowManager.LayoutParams.LAST_SYSTEM_WINDOW) {
return WindowManagerImpl.ADD_OKAY;
}
String permission = null;
switch (type) {
case TYPE_TOAST:
// XXX right now the app process has complete control over
// this... should introduce a token to let the system
// monitor/control what they are doing.
break;
case TYPE_INPUT_METHOD:
case TYPE_WALLPAPER:
// The window manager will check these.
break;
case TYPE_PHONE:
case TYPE_PRIORITY_PHONE:
case TYPE_SYSTEM_ALERT:
case TYPE_SYSTEM_ERROR:
case TYPE_SYSTEM_OVERLAY:
permission = android.Manifest.permission.SYSTEM_ALERT_WINDOW;
break;
default:
permission = android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
}
if (permission != null) {
if (mContext.checkCallingOrSelfPermission(permission)
!= PackageManager.PERMISSION_GRANTED) {
return WindowManagerImpl.ADD_PERMISSION_DENIED;
}
}
return WindowManagerImpl.ADD_OKAY;
}
明显不适合的 Type:TYPE_TOAST, TYPE_INPUT_METHOD, TYPE_WALLPAPER; 可能适合的 Type:TYPE_PHONE,
TYPE_PRIORITY_PHONE, TYPE_SYSTEM_ALERT, TYPE_SYSTEM_ERROR, TYPE_SYSTEM_OVERLAY; 其它类型的 Type:
需要系统签名权限:
android.Manifest.permission.INTERNAL_SYSTEM_WINDOW
而申请该权限需要系统签名,所以我们是无法获取权限的。
TYPE_PHONE
/**
* Window type: phone. These are non-application windows providing
* user interaction with the phone (in particular incoming calls).
* These windows are normally placed above all applications, but behind
* the status bar.
* In multiuser systems shows on all users' windows.
*/
public static final int TYPE_PHONE = FIRST_SYSTEM_WINDOW+2;
TYPE_PHONE 类型的窗口可以显示在其它 APP 的上面,但不能显示在锁屏的上面,所以 PASS。
TYPE_PRIORITY_PHONE
评论0