Android 8 在service中弹出dialog失败

Android 8 在service中弹出dialog失败,

需要对dialog设置(红色部分)

public void showDownProgress() {
    if (dialog == null) {

        Activity activity = MainApplication.getTopActivity();

        View view = LinearLayout.inflate(activity, R.layout.dialog_update_progress, null);
        dialog = new Dialog(mContext, R.style.update_dialog);
        dialog.setContentView(view);
        tv_value = view.findViewById(R.id.tv_value);
        progressBar = view.findViewById(R.id.progressBar);
        progressBar.setMax(100);


        DisplayMetrics dm = getResources().getDisplayMetrics();
        int displayWidth = dm.widthPixels;
        int displayHeight = dm.heightPixels;

        android.view.WindowManager.LayoutParams p = dialog.getWindow().getAttributes();
        p.width = (int) (displayWidth * 0.9); 
        p.height = (int) (displayHeight * 0.2); 

        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.getWindow().setAttributes(p); 

        if(Build.VERSION.SDK_INT == 27) {
            dialog.getWindow().setType((WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY));
        }else {
            dialog.getWindow().setType((WindowManager.LayoutParams.TYPE_SYSTEM_ALERT));
        }

        dialog.show();
    }
}

你可能感兴趣的:(实用技巧)