最佳答案
我的工作在一个应用程序,我需要显示一些信息 开始的锁屏幕(钥匙保护)没有解锁的手机窗口。我想用 TYPE _ KEYGUARD _ DIALOG也许能做到
但是每次我的应用程序崩溃时都会出现以下错误:
视图.WindowManager $BadTokenException: 无法添加窗口 ViewRootImpl $W@40ec8528——拒绝此许可 窗口类型
这些帖子(给你,给你和 给你)都给出了相同的答案。 在 Manifest 文件中添加以下权限。
许可
解决方案,我已经实现,但我仍然得到相同的错误。 知道我哪里做错了吗?
以下是清单文件中的权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.droidpilot.keyguardwindow" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
这是我用来将窗口添加到锁屏的代码
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = mInflater.inflate(R.layout.lock_screen_notif, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
PixelFormat.TRANSLUCENT
);
wm.addView(mView, params);
有人知道吗?
另外,我正在测试一台运行 Android 4.4.2的 HTC Desire 620DS