0

I have create alert dialog in to method onProgressUpdate of Asynctask and i have put permission in to manifest:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

When app shows alert dialog i receive this exception:

android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@e9ea0e -- permission denied for this window type

Code is:

   @Override
protected void onProgressUpdate(Object[] values) {

    this.holder.itemView.setBackgroundColor(Color.WHITE);
    if(messaggio){

       // Toast.makeText(context, testoMessaggio, Toast.LENGTH_SHORT).show();

        final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle(nameFile);
        alertDialog.setMessage(testoMessaggio);
        alertDialog.setCancelable(true);


        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //alertDialog.cancel();
            }
        });
        alertDialog.show();

    }
    super.onProgressUpdate(values);

}

Why i have this exception?

If i remove this:

alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //alertDialog.cancel();
            }
        });

it works why?

onProgressUpdate i have try create notification but it doesn't work:

    @Override
protected void onProgressUpdate(Object[] values) {

    this.holder.itemView.setBackgroundColor(Color.WHITE);
    if(messaggio){

        PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, EUDroid_Main_Activity.class), 0);
        Notification notification = new NotificationCompat.Builder(context)
                .setTicker("Ticker")
                .setContentTitle("Title")
                .setContentText("Hello")
                .setContentIntent(pi)
                .setAutoCancel(true)
                .build();

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, notification);


    }
    super.onProgressUpdate(values);

}

why?

Fra87
  • 297
  • 2
  • 5
  • 16

1 Answers1

0

try this , this might help you. I don know how much is it reliable but my app is not crashing now.

windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
    //here is all the science of params
    final LayoutParams myParams = new LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            LayoutParams.TYPE_SYSTEM_ERROR,
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            PixelFormat.TRANSLUCENT
    );

In your manifest file just give the permission

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Addition to this you can also check for the API level if its >=23 then

if(Build.VERSION.SDK_INT >= 23) {
    if (!Settings.canDrawOverlays(Activity.this)) {
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, 1234);
    }
}
            else
{
    Intent intent = new Intent(Activity.this, Service.class);
    startService(intent);
}

I hope it helps someone somewhere.