问题描述
我正在尝试创建系统覆盖.但我不断收到权限被拒绝".我使用的是 SDK 版本 23.
I'm trying to create a system overlay. But I keep getting "permission denied". I'm using SDK version 23.
我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test" >
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".activity.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我用来创建叠加层的代码:
The code I use to create the overlay:
//mView = new HUDView(this);
mButton = new Button(this);
mButton.setText("Overlay button");
mButton.setOnTouchListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(mButton, params);
推荐答案
首先,没有名为SYSTEM_OVERLAY_WINDOW
的权限.它是SYSTEM_ALERT_WINDOW
.
First, there is no permission named SYSTEM_OVERLAY_WINDOW
. It is SYSTEM_ALERT_WINDOW
.
其次,如果您的 targetSdkVersion
为 23 或更高,并且您在 Android 6.0+ 设备上运行,您的应用一开始将不会获得此权限.调用 Settings.canDrawOverlays()
看看你是否有权限,如果没有,使用 ACTION_MANAGE_OVERLAY_PERMISSION
引导用户进入设置.
Second, if your targetSdkVersion
is 23 or higher, and you are running on Android 6.0+ devices, your app will not get this permission at the outset. Call Settings.canDrawOverlays()
to see if you have the permission, and use ACTION_MANAGE_OVERLAY_PERMISSION
to lead the user over to Settings if you do not.
这篇关于Android系统覆盖窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!