Reputation: 1105
I have an windows manager inside the broadcast receiver and am using API > 23 so i provided overlay permission in the main activity like below.
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivity(myIntent);
}
After providing the permission also am getting the below error .
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@3decac7 -- permission denied for this window type.
window manager used in the broadcast receiver
WindowManager.LayoutParams params = new
WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
PixelFormat.TRANSLUCENT);
params.gravity= Gravity.CENTER;
params.x=0;
params.y=0;
windowManager.addView(look, params);
provided permissionin Manifest also.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
please help how to proceed.
Upvotes: 4
Views: 3088
Reputation: 3472
You have a bad layout type ( WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG
).
You can use TYPE_APPLICATION_OVERLAY
for api >= 26 and TYPE_TOAST
for lower api.
Also you can read the WindowManager.LayoutParams api doc to know wich type you can use
Upvotes: 1