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); 

} 

为什么呢?

+0

检查此问题http://stackoverflow.com/questions/32224452/android-unable-to-add-window-permission-denied-for-this-window-type – Raghavendra

+0

我已尝试通知(查找),但不'为什么? – Fra87

回答

0

试试这个,这可能对你有帮助。我不知道它有多可靠,但我的应用程序现在没有崩溃。

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 
    ); 

在您的清单文件只是给你许可

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

除此之外,您还可以检查API级别如果> = 23,然后

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); 
} 

我希望它能帮助有人在某个地方。

相关问题