I have Point of Service device this device has dual screen
When the target api is less than 22, It only worked if you had the permission . but Problem occurs when target api is 22 or higher problem is permission denied for this window type
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();
}
});
}
}
this is my logs
04-01 09:55:18.042 16389-16389/com.example.dualscreen E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.dualscreen, PID: 16389 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dualscreen/com.example.dualscreen.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@f70b45a -- permission denied for this window type at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2421) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:772) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662) Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@f70b45a -- permission denied for this window type at android.view.ViewRootImpl.setView(ViewRootImpl.java:591) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) at android.app.Dialog.show(Dialog.java:319) at android.app.Presentation.show(Presentation.java:235) at com.example.dualscreen.MainActivity.onCreate(MainActivity.java:39) at android.app.Activity.performCreate(Activity.java:6266) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2374) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1346) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5425) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:772) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
{}
button) and do not spam with irrelevant tags. – Marcin Orlowski