异步任务来显示一个AlertDialog任务、AlertDialog

2023-09-12 08:18:28 作者:清欢百味

在过去的几天里,我一直没能解决的问题,我的对话框。我运行一个线程来显示5000毫秒对话框并删除它。我试图表现出吐司(SUCCESS)。问题是我得到的对话框中第二次也。我是新android开发,我需要解决这个与异步任务,这样我可以推迟第二个线程,并显示了积极的按钮而不是吐司警报dialog.builder。我瞪大眼睛了很多,但我困惑实现这个

在这里,我派我的凭据的服务器和在发送我表示5000毫秒进度对话框,我想有一个单独的线程,以示与积极的按钮dialog.builder。(当用户拿到反应在logcat中的,我应该检查响应code == 1或不从的logcat显示生成器)

PLZ有人能帮我解决这个有些code段如果可能的话。

在此先感谢。

这是在code,我需要实现异步任务

 按钮的登入=(按钮)findViewById(R.id.regsubmitbtn);
    signin.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            //的ShowDialog(0);
            T =新的Thread(){
                公共无效的run(){
                    寄存器();
                    尝试 {
                        而(计数器2){
                            showmsg(0);
                            视频下载(5000);
                            removeDialog(0);
                            // Toast.makeText(Register.thisRegisterd,Toast.LENGTH_LONG).show();
                            showmsg(1);
                            // toast.show();

                            视频下载(1000);

                        }

                    }赶上(InterruptedException异常E){
                        e.printStackTrace();
                    }
                }
            };
            t.start();
        }
    });

}

@覆盖
受保护的对话框onCreateDialog(INT ID){
    开关(ID){
    情况下0:{
        ++计数器;
        对话框=新ProgressDialog(本);
        如果(计数器== 1){
            dialog.setMessage(注册...);
        }
        其他 {
            字符串resultsRequestSOAP =;
            如果(Log.v(TAG,将String.valueOf(resultsRequestSOAP))== 1)
                ;
            {
                上下文的背景下= getApplicationContext();
                CharSequence的文字=Registerd;
                INT持续时间= Toast.LENGTH_LONG;

                吐司面包= Toast.makeText(背景,文本,持续时间);
                toast.show();
            }
        }
        dialog.setIndeterminate(真正的);
        dialog.setCancelable(真正的);
        返回对话框;
    }
    }
    返回null;
}

公共无效showmsg(INT actionsToBePerformedOnScreen){
    消息味精=新的Message();
    msg.what = actionsToBePerformedOnScreen;
    handler.sendMessage(MSG);

}

公开处理程序处理程序=新的处理程序(){
    @覆盖
    公共无效的handleMessage(信息MSG){
        开关(msg.what){
        情况下0:

            的ShowDialog(0);

            打破;

        情况1:
            //清除列表中的所有图像
            removeDialog(0);
            打破;

        }

    };
};
 

解决方案

很简单:显示对话框在preExecute ,寄存器()在 onPostExecute doInBackground 和隐藏对话框。最后,执行新RegisterTask()。执行()的onclick

 私有类RegisterTask扩展的AsyncTask<虚空,虚空,布尔> {
  私人最终ProgressDialog对话框=新ProgressDialog(YourClass.this);

  在preExecute保护无效(){
     this.dialog.setMessage(登录...);
     this.dialog.show();
  }

  保护布尔doInBackground(最终虚空未使用){
     返回Main.this.register(); //不与UI交互!
  }

  保护无效onPostExecute(最终布尔结果){
     如果(this.dialog.isShowing()){
        this.dialog.dismiss();
     }
     如果(result.booleanValue()){
     //还显示注册成功的对话框
     }
  }
}
 
如何在Android应用中使用AlertDialog实现一个对话框

For the past few days, I haven't been able to solve an issue with my dialog box. I am running a thread to show the dialog box for 5000ms and removing it. and I am trying to show a toast("SUCCESS"). The problem is I am getting the dialog box for the second time also. I am new to android development and I need to solve this with Async Task so that I can delay the second thread and show a alert dialog.builder with a positive button instead of toast. I goggled a lot but I confused to to implement this

Here I am sending my credentials to server and while sending I am showing a progress dialog box for 5000ms and I want to have a separate thread in order to show the dialog.builder with a positive button.( When the user get a response in the logcat for that I should check the responsecode==1 or not from the logcat to show the builder)

plz someone help me to solve this with some code snippet if possible.

Thanks in advance.

this is the code where I need to implement Async task

Button signin = (Button) findViewById(R.id.regsubmitbtn);
    signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // showDialog(0);
            t = new Thread() {
                public void run() {
                    register();
                    try {
                        while (counter < 2) {
                            showmsg(0);
                            Thread.sleep(5000);
                            removeDialog(0);
                            // Toast.makeText(Register.this, "Registerd", Toast.LENGTH_LONG).show();
                            showmsg(1);
                            // toast.show();

                            Thread.sleep(1000);

                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            t.start();
        }
    });

}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 0: {
        ++counter;
        dialog = new ProgressDialog(this);
        if (counter == 1) {
            dialog.setMessage("Registering...");
        }
        else {
            String resultsRequestSOAP = "";
            if (Log.v("TAG", String.valueOf(resultsRequestSOAP)) == 1)
                ;
            {
                Context context = getApplicationContext();
                CharSequence text = "Registerd";
                int duration = Toast.LENGTH_LONG;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
        }
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        return dialog;
    }
    }
    return null;
}

public void showmsg(int actionsToBePerformedOnScreen) {
    Message msg = new Message();
    msg.what = actionsToBePerformedOnScreen;
    handler.sendMessage(msg);

}

public Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 0:

            showDialog(0);

            break;

        case 1:
            // clear all images in the list
            removeDialog(0);
            break;

        }

    };
};

解决方案

Easy: show dialog onPreExecute, register() in doInBackground and hide dialog in onPostExecute. Finally, do new RegisterTask().execute() in your onclick.

 private class RegisterTask extends AsyncTask<Void, Void, Boolean> {
  private final ProgressDialog dialog = new ProgressDialog(YourClass.this);

  protected void onPreExecute() {
     this.dialog.setMessage("Signing in...");
     this.dialog.show();
  }

  protected Boolean doInBackground(final Void unused) {
     return Main.this.register(); //don't interact with the ui!
  }

  protected void onPostExecute(final Boolean result) {
     if (this.dialog.isShowing()) {
        this.dialog.dismiss();
     }
     if (result.booleanValue()) {
     //also show register success dialog
     }
  }
}