我试图创建一个高于其他应用程序的视图:
WindowManager.LayoutParams paramsDirectorView = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
我查看了其他回复,发现了以下“绘制应用程序”的内容:
我仍然得到“ - 窗口类型2038的权限被拒绝”错误。截至目前我正在使用TYPE_PHONE并且它可以工作,但它已被弃用并且说要使用TYPE_APPLICATION_OVERLAY。有人可以对此进行跟进,因为TYPE_PHONE答案不是真正的解决方案,而是Android O中不推荐使用的“补丁工作”解决方案。
我在Android 7.1.2上运行
android.view.WindowManager $ BadTokenException:无法添加窗口android.view.ViewRootImpl$W@1f47e89 - android.app.ActivityThread上android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3344)的窗口类型2038的权限被拒绝.- wrap21(ActivityThread.java)位于android.app.AooT.Thread上的android.O. HandleMessage(Handler.java:102)上的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1583)。(Looper) .java:154)在android.app.ActivityThread.main(ActivityThread.java:6121)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit) .java:889)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)引起:android.view.WindowManager $ BadTokenException:无法添加窗口android.view.ViewRootImpl$W@1f47e89 - 在Android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)的android.view.ViewRootImpl.setView(ViewRootImpl.java:703)上的窗口类型2038的权限被拒绝HeadService.TwoViewManager的android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)。(两个ViewManager.java:99)位于android.app.ActivityThread.handleServiceArgs(ActivityThread)的HeadService.UIHeadService.onStartCommand(UIHeadService.java:65)。 java:3327)在Android.app.Handler.dispatchMessage(Handler.java:102)的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1583)的android.app.ActivityThread.-wrap21(ActivityThread.java)在android.os.Looper.loop(Looper.java:154)的android.app.ActivityThread.main(ActivityThread.java:6121),位于com.android.internal的java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.ZygoteInit.main。(ZygoteInit.java:779
我有完全相同的问题。我想你应该区分目标(奥利奥之前和之后)
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
我在服务类(Marshmallow之前和之后)有完全相同的问题。
if (Build.VERSION.SDK_INT >= 23) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 1234);
}
} else {
startService(new Intent(SplashActivity.this,
CheckServicesForApps.class));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1234) {
startService(new Intent(SplashActivity.this,
CheckServicesForApps.class));
}
}
public class CheckServicesForApps extends Service {
private Context context = null;
@Override
public void onCreate() {
super.onCreate();
ImageView imageView = new ImageView(context);
imageView.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
//here is all the science of params
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager. LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
PixelFormat.TRANSLUCENT
);
windowManager.addView(imageView, params);
hand=new Handler();
} catch (Exception e) {
hand=new Handler();
e.printStackTrace();
}
}else{
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.CENTER;
params.x = ((getApplicationContext().getResources().getDisplayMetrics().widthPixels) / 2);
params.y = ((getApplicationContext().getResources().getDisplayMetrics().heightPixels) / 2);
windowManager.addView(imageView, params);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
/* We want this service to continue running until it is explicitly
* stopped, so return sticky.
*/
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if (imageView != null) {
try {
windowManager.removeView(imageView);
} catch (Exception e) {
e.printStackTrace();
}
}
/**** added to fix the bug of view not attached to window manager ****/
}
}
您是否通过调用以下intent来请求运行时权限?
private void requestOverlayPermission() {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M) {
return;
}
Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
myIntent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(myIntent, APP_PERMISSIONS);
}
然后在onActivityResult()中检查Settings.canDrawOverlays(this)是否为true,否则通过调用above方法再次请求权限。
Source SYSTEM_ALERT_WINDOW字符串SYSTEM_ALERT_WINDOW允许应用使用类型TYPE_APPLICATION_OVERLAY创建窗口,显示在所有其他应用之上。很少有应用程序应该使用此权限;这些窗口用于与用户进行系统级交互。
注意:如果应用面向API级别23或更高级别,则应用用户必须通过权限管理屏幕向应用明确授予此权限。该应用程序通过发送动作ACTION_MANAGE_OVERLAY_PERMISSION来请求用户的批准。该应用可以通过调用Settings.canDrawOverlays()来检查它是否具有此授权。
尝试将WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
改为WindowManager.LayoutParams.TYPE_PHONE
?