您好,我收到此异常:
无法添加窗口android.view.ViewRootImpl$W@b65df5f-权限 窗口类型为2010拒绝
我已经添加了android.permission.SYSTEM_ALERT_WINDOW
这是我的清单文件
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:hardwareAccelerated="true"
android:largeHeap="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name="com.lockscreen.wipe.MainActivity$DeviceAdminSampleReceiver"
android:description="@string/sample_device_admin_description"
android:label="@string/sample_device_admin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin_sample" />
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
<receiver
android:name="com.lockscreen.wipe.LockscreenIntentReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
...
</manifest>
Java:
public class LockscreenUtils {
// Member variables
private OverlayDialog mOverlayDialog;
private OnLockStatusChangedListener mLockStatusChangedListener;
// Interface to communicate with owner activity
public interface OnLockStatusChangedListener
{
public void onLockStatusChanged(boolean isLocked);
}
// Reset the variables
public LockscreenUtils() {
reset();
}
// Display overlay dialog with a view to prevent home button click
public void lock(Activity activity) {
if (mOverlayDialog == null) {
mOverlayDialog = new OverlayDialog(activity);
mOverlayDialog.show();
mLockStatusChangedListener = (OnLockStatusChangedListener) activity;
}
}
// Reset variables
public void reset() {
if (mOverlayDialog != null) {
mOverlayDialog.dismiss();
mOverlayDialog = null;
}
}
// Unlock the home button and give callback to unlock the screen
public void unlock() {
if (mOverlayDialog != null) {
mOverlayDialog.dismiss();
mOverlayDialog = null;
if(mLockStatusChangedListener!=null)
{
mLockStatusChangedListener.onLockStatusChanged(false);
}
}
}
// Create overlay dialog for lockedscreen to disable hardware buttons
private static class OverlayDialog extends AlertDialog {
public OverlayDialog(Activity activity) {
super(activity, R.style.OverlayDialog);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.type = LayoutParams.TYPE_SYSTEM_ERROR;
params.dimAmount = 0.0F;
params.width = 0;
params.height = 0;
params.gravity = Gravity.BOTTOM;
getWindow().setAttributes(params);
getWindow().setFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED | LayoutParams.FLAG_NOT_TOUCH_MODAL,
0xffffff);
setOwnerActivity(activity);
setCancelable(false);
}
// consume touch events
public final boolean dispatchTouchEvent(MotionEvent motionevent) {
return true;
}
}
}
LockscreenUtils.lock()引发异常...
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lockscreen.wipe, PID: 8803
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lockscreen.wipe/com.lockscreen.wipe.LockScreenActivity}: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@ffd0980 -- permission denied for window type 2010
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@ffd0980 -- permission denied for window type 2010
at android.view.ViewRootImpl.setView(ViewRootImpl.java:789)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:330)
at com.lockscreen.wipe.LockscreenUtils.lock(LockscreenUtils.java:33)
at com.lockscreen.wipe.LockScreenActivity.lockHomeButton(LockScreenActivity.java:178)
at com.lockscreen.wipe.LockScreenActivity.onCreate(LockScreenActivity.java:75)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)