0
我在來的AsyncTask的方法onProgressUpdate創建警報對話框,我已經在把權限來體現:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
應用程序時顯示警告對話框我收到此異常:
android.view.WindowManager$BadTokenException: Unable to add window [email protected] -- permission denied for this window type
代碼是:
@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);
}
爲什麼我有這個例外?
如果我刪除此:
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//alertDialog.cancel();
}
});
它的工作原理是爲什麼?
onProgressUpdate我已經嘗試建立通知,但它不工作:
@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);
}
爲什麼呢?
檢查此問題http://stackoverflow.com/questions/32224452/android-unable-to-add-window-permission-denied-for-this-window-type – Raghavendra
我已嘗試通知(查找),但不'爲什麼? – Fra87