爱网站在线观看视频,国际网站建设与维护,只做公司网站方案,怎样上传自己的网站前言想在锁屏上面实现弹窗#xff0c;第一个想法就是利用 WindowManager设置 Window的 Flag#xff0c;通过设置 Flag的显示优先级来让窗口显示在锁屏的上面。接下来就是试验可能相关的 Window Type属性#xff0c;验证该方案是否可行。在尝试各个 Window Type 属性之前需要…前言想在锁屏上面实现弹窗第一个想法就是利用 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;}明显不适合的 TypeTYPE_TOAST, TYPE_INPUT_METHOD, TYPE_WALLPAPER; 可能适合的 TypeTYPE_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_WINDOW2;TYPE_PHONE 类型的窗口可以显示在其它 APP 的上面但不能显示在锁屏的上面所以 PASS。TYPE_PRIORITY_PHONE/*** Window type: priority phone UI, which needs to be displayed even if* the keyguard is active. These windows must not take input* focus, or they will interfere with the keyguard.* In multiuser systems shows on all users windows.*/public static final int TYPE_PRIORITY_PHONE FIRST_SYSTEM_WINDOW7;TYPE_PRIORITY_PHONE 类型的窗口可以显示在其它 APP 的上面但不能显示在锁屏的上面所以 PASS。而且实际的行为和注释并不相符该类型的窗口是可以获取交互事件的具体原因待查。TYPE_SYSTEM_ALERT/*** Window type: system window, such as low power alert. These windows* are always on top of application windows.* In multiuser systems shows only on the owning users window.*/public static final int TYPE_SYSTEM_ALERT FIRST_SYSTEM_WINDOW3;TYPE_SYSTEM_ALERT类型的窗口可以显示在其它 APP 的上面但不能显示在锁屏的上面所以 PASS。TYPE_SYSTEM_OVERLAY/*** Window type: system overlay windows, which need to be displayed* on top of everything else. These windows must not take input* focus, or they will interfere with the keyguard.* In multiuser systems shows only on the owning users window.*/public static final int TYPE_SYSTEM_OVERLAY FIRST_SYSTEM_WINDOW6;TYPE_SYSTEM_OVERLAY 类型的窗口可以显示在所有其它窗口的上面包括锁屏而且不会影响它下面窗口的交互事件响应但是该属性窗口不能获得焦点无法进行交互(如果该窗口可以获取焦点那么就可以用来抓取用户的锁屏密码出于安全考虑系统是不会允许的)所以只能用来简单的展示内容如果需要交互的锁屏弹窗那么该属性 PASS。TYPE_SYSTEM_ERROR/*** Window type: internal system error windows, appear on top of* everything they can.* In multiuser systems shows only on the owning users window.*/public static final int TYPE_SYSTEM_ERROR FIRST_SYSTEM_WINDOW10;在原生 ROM 5.1 下试验是可以显示出来的但根据注释来看(appear on top of everything they can)不是在所有情况下都可以显示在锁屏上面的而且像 MIUI 和 Flyme 等 ROM 默认是屏蔽浮窗权限的考虑到这点利用 WindowManager添加浮窗的方式实现锁屏弹窗的方案基本 PASS。使用 Activity 的方式实现首先需要对 Activity 进行如下设置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);}其中最主要也是必须要设置的就是FLAG_SHOW_WHEN_LOCKED顾名思义就是锁屏下显示该 Activity。而其它几个 Flag包括解锁、保持屏幕常亮、点亮屏幕可以根据具体的需求选择设置。在 AndroidManifest.xml 中声明 Activity同样该 Activity也需要在 AndroidManifest.xml中声明声明时需注意添加 android:excludeFromRecentstrue 属性是为了将该 Activity从最近任务列表中去除否则用户会觉得很奇怪。还有因为这个 Activity会整个盖在锁屏上面而且就算设置成背景透明锁屏界面也不会显示在下面(系统主要是出于安全考虑)所以需要考虑下该 Activity的背景这里为了显示不要太突兀将主题设为壁纸。android:launchModesingleInstanceandroid:excludeFromRecentstrueandroid:themeandroid:style/Theme.Wallpaper.NoTitleBar/启动 Activity由于该 Activity是为了在锁屏的情况下显示的所以启动 Activity时不要忘了判断手机是否处于锁屏状态可以通过下面这种方式判断锁屏状态KeyguardManager km (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);if (km.inKeyguardRestrictedInputMode()) {// 处于锁屏状态}总结以上就是在Android中实现锁屏状态下弹窗效果的全部内容希望本文的内容对大家开发Android的时候能有所帮助如果有疑问欢迎大家留言讨论。