在此应用程序中,用户登录并根据服务器检查其凭据。
用户可能会等待几秒钟,这取决于手机打开数据连接的速度(如果有的话)。我需要一个对话框说“请稍候”或“验证凭据”或在用户单击登录后长的那些行。
所需的可视化顺序:按“登录”->“请稍候”对话框将显示在此同一活动中->当结果来自服务器时,将加载新活动(或引发错误)
当前可视顺序:按“登录”->“用户等待”,如同应用程序已冻结->已加载新活动
我试着用asynctask做这个线程处理,但是我现在还没有得到它!

class Progressor extends AsyncTask<Void, Void, Void> {

ProgressDialog dialog;
    protected void onPreExecute(){
        dialog = ProgressDialog.show(Login.this, "Logging In",
                "Verifying Credentials, Please wait...", true);
    }

然后在我的oncreate方法中,我有了所有其他的逻辑,比如用户单击按钮之类的,但是我已经把它移到了asynctask方法的doinbackground函数中
  /* When the Login Button is clicked: */
        Button loginButton = (Button) findViewById(R.id.loginButton);
        loginButton.setOnClickListener(new OnClickListener() {


            public void onClick(View v) {

                Progressor showMe = new Progressor();
                showMe.onPreExecute();
                showMe.doInBackground(null);
                showMe.onPostExecute();

OnPostExecute只需关闭对话框
为什么这不起作用,应该如何重新安排。我应该向showme.doinbackground()函数传递什么变量,它是空的。在调试过程中,它从不进入这里
    @Override
    protected Void doInBackground(Void... arg0) {

最佳答案

这不是你使用AsyncTask的方式,看看documentation。创建任务的新实例后,只需调用execute(),而不是单个方法:

Progressor showMe = new Progressor();
showMe.execute();

08-04 09:34
查看更多