2016-08-10 60 views
0

我在API 23上的自定義對話框中遇到了一個問題,它正在崩潰。爲什麼自定義對話框不適用於API 23?

final Dialog dialog = new Dialog(getActivity()); 
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.progress_dialog); 
dialog.setCancelable(false); 
dialog.show(); 

崩潰報告:

android.view.WindowManager$BadTokenException: Unable to add window [email protected] -- permission denied for this window type 
at android.view.ViewRootImpl.setView(ViewRootImpl.java:598) 
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310) 
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:86) 
at android.app.Dialog.show(Dialog.java:319) 
at com.example.hello.fragment.ExploreFragment.exploreBeers(ExploreFragment.java:255) 
at com.example.hello.fragment.ExploreFragment$4.onEditorAction(ExploreFragment.java:203) 
at android.widget.TextView.onEditorAction(TextView.java:4773) 
at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:139) 
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:304) 
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:152) 
at android.app.ActivityThread.main(ActivityThread.java:5497) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

清單權限:

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

它不是在所有的工作我感到有點迷惑,請您通過我的職務,並建議我一些解決方案。

+0

權限被拒絕此窗口類型。在啓動該功能之前,您需要詢問運行時間權限。 –

+0

嘿謝謝你能告訴我如何做到這一點? –

+0

使用LayoutParams.TYPE_TOAST或TYPE_APPLICATION_PANEL – siddhesh

回答

0

刪除 dialog.getWindow()。setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 並嘗試在您的應用程序。 或者如果你想顯示其在第三方應用程序

+0

嘿感謝它的工作 –

+0

Manifest Permission: 需要當你想顯示對話框當你的應用程序在後臺或應用程序關閉 – Sumit

0

看看我answer on runtime permissions

在這個答案,您可以在訪問聯繫人方法你的代碼。

在任何活動中實現權限的示例。

SampleActivity.java

public class SampleActivity extends AppCompatActivity{ 
     private final int PERMISSION_CODE = 1; 
     Button button; 
     @override 
     onCreate(Bundle savedInstance){ 
      super.onCreate(savedInstance); 
      setContentView(R.layout.your_layout); 
      button = (Button)findViewById(R.id.button); 
      button.setOnClickListener(new OnClickListener(){ 
       @override 
       public void onClick(View view){ 
        requestPermissionAndContinue(); 
       } 
      }); 
      //remaining code to continue using the app 
      //your actual code should also be in this same class 
     } 

     private void requestPermissionAndContinue(){ 
      if(ContextCompat.checkSelfPermission(this, Manifest.permission.SYSTEM_ALERT_WINDOW) != PackageManager.PERMISSION_GRANTED){ 
       if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SYSTEM_ALERT_WINDOW)){ 
        Log.e(TAG, "permission denied, show dialog"); 
       }else{ 
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW}, PERMISSION_CODE); 
       } 
      }else{ 
       showAlert(); 
      } 
     } 

     private void showAlert(){ 
      //your code to show alert dialog 
      final Dialog dialog = new Dialog(getActivity()); 
    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.progress_dialog); 
dialog.setCancelable(false); 
dialog.show(); 
     } 

     @Override 
     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
      if(grantResults.length > 0 && permissions[0]==PackageManager.PERMISSION_GRANTED){ 
       showAlert(); 
      }else{ 
       //redirect to settings page or ask permission again 
      } 
     } 
    }