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?