android - 有没有一种方法可以在AsyncTask中显示AlertDialog?

标签 android android-asynctask android-alertdialog

我想显示 AsyncTask 中其他类中的 AlertDialog。 示例>

public class WardListAsyncTask extends AsyncTask<HashMap<String, String>, String, Object> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Object doInBackground(HashMap<String, String>... params) {
      ...
    }

   @Override
    protected void onPostExecute(Object result) {
        ConfirmAlertDialog(ez_WardList.this,"HI?"); 
        //this method is in another Class & I want to use this method another Asynctask also..
    }

ConfirmAlertDialog 是...

    Context g_ctx;
String g_content;
public void ez_ConfirmAlertDialog(Context ctx, String content) {
    this.g_ctx=ctx;
    this.g_content=content;

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(g_ctx, AlertDialog.THEME_HOLO_LIGHT);
            builder.setMessage(g_content).setPositiveButton(getString(R.string.kor_confirm),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                        }
                    });

            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            alertDialog.getWindow().setLayout(displayWidth / 2, LayoutParams.WRAP_CONTENT);
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.rgb(10, 174, 239));

        }
    });

}

我认为 g_ctx.class.runonuiThread ...但我无法调用 runonuithread...

如何解决?

最佳答案

Activity 类中存在 runOnUiThread 方法。因此应该将 Activity 传递到您的类(class)中。 示例:

public class MyClass{


public void ez_ConfirmAlertDialog(Activity activity, String content) {
    this.g_ctx=ctx;
    this.g_content=content;

    if(activity == null){
       return;
    }

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(g_ctx, AlertDialog.THEME_HOLO_LIGHT);
            builder.setMessage(g_content).setPositiveButton(getString(R.string.kor_confirm),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                        }
                    });

            AlertDialog alertDialog = builder.create();
            alertDialog.show();
            alertDialog.getWindow().setLayout(displayWidth / 2, LayoutParams.WRAP_CONTENT);
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.rgb(10, 174, 239));

        }
    });

}

}

关于android - 有没有一种方法可以在AsyncTask中显示AlertDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39161791/

相关文章:

android - 为基于邻近度的移动应用程序创建自己的信标服务

android - 为什么 EditText.getText 在 Android 中显示为 null?

android - 即使 Activity 已销毁,AsyncTask 也不会停止

Android 嵌套的 AlertDialog - 这可能吗?

android - 如何设置对话框外边距? (安卓)

android - 带有自定义子项的可扩展 ListView

android - AsyncTask 到 SharedPreferences

android - NullPointer 与 Geocoder/AsyncTask - Android

java - 具有自定义 View : Resize to wrap the view's content 的 AlertDialog

java - 如何共享fragment和activity之间的连接