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

我希望它能幫助有人在某個地方。

相關問題