OnClick事件结束后启动AlertDialog

时间:2018-07-20 10:45:41

标签: android onclicklistener android-alertdialog

在按钮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();
                }
            });

3 个答案:

答案 0 :(得分:1)

获取上次卸载的结果后,可以在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();
    }

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

请尝试这个

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