锁屏界面无法无法显示Toast

来源:互联网 发布:养生网源码 编辑:程序博客网 时间:2024/05/18 19:37

在锁屏界面开发几个快捷方式启动某些应用,例如:Calculator, Camera,Google voice和Call等,界面如下。且这种做法很普遍,很多厂商都有。


说明:

1>左边图片右下角为快速启动Calculator的快捷方式,在无需解锁的情况下,进入Calcaulator应用。

2>右边图片为从launcher中启动Calculator后,操作时弹出的Toast提示。

那么问题来了,当不解锁启动Calculator后,同操作无法显示Toast内容。


1.这里搜寻API,得到如下结果,本问题的核心也正在此。

/frameworks/base/core/java/android/view/WindowManager.java

        /** Window flag: special flag to let windows be shown when the screen         * is locked. This will let application windows take precedence over         * key guard or any other lock screens. Can be used with         * {@link #FLAG_KEEP_SCREEN_ON} to turn screen on and display windows         * directly before showing the key guard window.  Can be used with         * {@link #FLAG_DISMISS_KEYGUARD} to automatically fully dismisss         * non-secure keyguards.  This flag only applies to the top-most         * full-screen window.         */        public static final int FLAG_SHOW_WHEN_LOCKED = 0x00080000;
        /** In a multiuser system if this flag is set and the owner is a system process then this         * window will appear on all user screens. This overrides the default behavior of window         * types that normally only appear on the owning user's screen. Refer to each window type         * to determine its default behavior.         *         * {@hide} */        public static final int PRIVATE_FLAG_SHOW_FOR_ALL_USERS = 0x00000010;

用法如下,为此也写了个demo来验证此问题:


2.但是Toast的getWindowParams方法带@hide的隐藏接口,非公开API,于是尝试使用反射方法获取。


3.但是随之而来的又一个问题,WindowManager.LayoutParams的privateFlags也为非公开API。

        /**         * Control flags that are private to the platform.         * @hide         */        public int privateFlags;

4.针对隐藏属性API,似乎无计可施。想到这是一个预置的应用,若是拥有平台签名,即可随意访问public的非公共API。

此方案可以参考另外一片文档《Android APK获取平台系统签名权限》,此处不作详述。

5.作为Calculator,原本可以是一个很普通的应用,若单为了这个Toast去提升应用之权限,似乎有不妥。于是又找了另一个法子,通过Toast.java源码的阅读,发现:

=>在调用Toast.makeText()方法时,会取new Toast对象;


而Toast的构造方法中又New了一个TN对象


6.结果发现,TN构造方法中就已经开始对此类“窗口”进行flags初始化。


说明:

上图时进行修改后的方法,为了避免对所有默认无参构造生成的Toast产生影响,即增加一个“构造方法”,使之更为方便地为类似Calculator这种应用场景的Toast服务。


7.

最终解决效果,如果加上para.type = WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;将还能在Lockscreen中显示,如果用户快速返回退至Lock。



原创粉丝点击