OnClick 事件完成后启动 AlertDialog

Starting AlertDialog after OnClick event is finished

On Button ClickListener 我有一些特定的任务要做:从设备上卸载选定的应用程序,我想在完成卸载后立即显示 alertDialog。我做了以下操作,但问题是 AlertDialog 在单击按钮的同时出现。我希望循环先完成并启动警报对话框。我该怎么做?

 btnSelection.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    List<AppList> stList = ((CardViewDataAdapter) mAdapter).getAppList();
                    for (int i = 0; i < stList.size(); i++) {
                        AppList singleApp = stList.get(i);
                        if (singleApp.isSelected() == true) {
                            String app_pkg_name = singleApp.getPackageName();
                            int UNINSTALL_REQUEST_CODE = 1;
                            Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
                            intent.setData(Uri.parse("package:" + app_pkg_name));
                            intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
                            startActivityForResult(intent, UNINSTALL_REQUEST_CODE);
                        }
                    }
                   AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                    alertDialog.setTitle("Alert");
                    alertDialog.setMessage("Finished Uninstalling");
                    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    alertDialog.show();
                }
            });

您可以在获得上次卸载结果后在onActivityResult 中显示AlertDialog。

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //check if Result for last uninstallation.

        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Finished Uninstalling");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
    }

请试试这个

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==UNINSTALL_REQUEST_CODE) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Finished Uninstalling");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
    }
}

尝试在您的 Activity 上使用 Override onActivityResult,并在收到结果后显示对话框。

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == UNINSTALL_REQUEST_CODE){
        //  Show your dialog
    }
}