真正的android双重显示权限问题

时间:2019-04-01 01:59:32

标签: android

我有服务点设备 该设备具有双屏

当目标api小于22时, 只有得到您的许可,它才能起作用。 但是当目标api为22或更高时会出现问题 问题是此窗口类型的权限被拒绝

public class MainActivity extends AppCompatActivity {
   Context context;
   DisplayManager mDisplayManager;
   DifferentDisplay mPresentation;
   DifferentDisplay2 mPresentation2;
   Display[]  displays;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context = getApplicationContext();

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.SYSTEM_ALERT_WINDOW) == PackageManager.PERMISSION_GRANTED){

    } else{
        //this is not working
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW},0);
    }


    mDisplayManager =(DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
    displays = mDisplayManager.getDisplays();

    mPresentation = new DifferentDisplay(context,displays[1]);//displays[1] is the customer display)
    mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    mPresentation.show();

    Button button = findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mPresentation2 = new DifferentDisplay2(context,displays[1]);//displays[1] is the customer display)
            mPresentation2.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            mPresentation2.show();


        }
    });

}
}

这是我的日志

04-01 09:55:18.042 16389-16389 / com.example.dualscreen E / AndroidRuntime:FATAL EXCEPTION:main     程序:com.example.dualscreen,PID:16389     java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.dualscreen / com.example.dualscreen.MainActivity}:android.view.WindowManager $ BadTokenException:无法添加窗口android.view.ViewRootImpl$W@f70b45a-此窗口类型的权限被拒绝         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2421)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)         在android.app.ActivityThread.-wrap11(ActivityThread.java)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1346)         在android.os.Handler.dispatchMessage(Handler.java:102)         在android.os.Looper.loop(Looper.java:148)         在android.app.ActivityThread.main(ActivityThread.java:5425)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:772)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)      原因:android.view.WindowManager $ BadTokenException:无法添加窗口android.view.ViewRootImpl$W@f70b45a-此窗口类型的权限被拒绝         在android.view.ViewRootImpl.setView(ViewRootImpl.java:591)         在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)         在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)         在android.app.Dialog.show(Dialog.java:319)         在android.app.Presentation.show(Presentation.java:235)         在com.example.dualscreen.MainActivity.onCreate(MainActivity.java:39)         在android.app.Activity.performCreate(Activity.java:6266)         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2374)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)         在android.app.ActivityThread.-wrap11(ActivityThread.java)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1346)         在android.os.Handler.dispatchMessage(Handler.java:102)         在android.os.Looper.loop(Looper.java:148)         在android.app.ActivityThread.main(ActivityThread.java:5425)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:772)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)

1 个答案:

答案 0 :(得分:0)

 mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);

这样,您不需要权限。但是

mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);

使用它仍然会引起问题谁有更好的解决方案?