应用程序查询服务器时需要显示加载屏幕

发布于 2024-10-29 18:55:55 字数 1300 浏览 1 评论 0原文

在此应用程序中,用户登录并根据服务器检查其凭据。

用户可能需要等待几秒钟,具体取决于手机打开数据连接的速度(如果有的话)。我需要在用户单击登录后显示“请稍候”或“验证凭据”或很长的内容的对话框。

所需的视觉顺序:按登录 -> 。在同一活动中显示“请稍候”对话框 ->当结果从服务器传入时,将加载新活动(或引发错误)

当前视觉顺序:按登录 ->用户等待,就像应用程序被冻结一样 ->新活动已加载

我正在尝试使用 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) {

in this app, the user logs in and their credentials are checked against a server.

The user could be waiting a few seconds, depending on how fast the phone can open a data connection if at all. I need dialog box saying "please wait" or "verifying credentials" or something a long those lines after the user clicks log in.

Desired visual order: press log in -> "please wait" dialog is show in this same activity -> when result comes in from server a new activity is loaded (or error is thrown)

Current visual order: press log in -> user waits as if the app is frozen -> new activity is loaded

I'm trying to do this threading thing with AsyncTask but I'm just not getting it right now!

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

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

Then in my oncreate method I had all of the other logic like user clicking the button and stuff, but I've since moved that into the AsyncTask method's doInBackGround function

  /* 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();

and onPostExecute simply dismisses the dialog box

Why doesn't this work and how should it be re-arranged. What variable should I be passing into the showMe.doInBackGround() function, it is void. In debugging it never goes in here

    @Override
    protected Void doInBackground(Void... arg0) {

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一指流沙 2024-11-05 18:55:55

不要手动调用 AsyncTask 的 onPreExecute/doInBackground 方法;只需对其调用 execute() ,它就会从正确的线程中的正确位置调用所有方法。它违背了异步任务从 UI 线程同步调用其所有方法的全部目的(这就是您的示例代码所做的)。

Don't call the onPreExecute/doInBackground methods of an AsyncTask manually; just call execute() on it and it will call all your methods in the proper places from the correct threads. It defeats the entire purpose of an asynchronous task to call all of its methods synchronously from the UI thread (which is what your sample code does).

旧伤还要旧人安 2024-11-05 18:55:55

这不是使用 AsyncTask 的方式,请查看 文档。创建任务的新实例后,只需调用 execute(),而不是单独的方法:

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

That isn't how you use an AsyncTask, have a look at the documentation. Once you have created a new instance of your task, just call execute(), not the individual methods:

Progressor showMe = new Progressor();
showMe.execute();
合约呢 2024-11-05 18:55:55

我在应用程序开始时有类似的代码,我从服务器加载当前设置,它适用于我:

public static ProgressDialog verlauf;
public static String vmessage = "";
static Handler handler = new Handler();;

public static void initialize_system(final Context ctx)
    {
        verlauf = ProgressDialog.show(ctx, "Starte FISforAndroid..", "synchronisiere Einstellungen",true,false);
        new Thread(){
             @Override
            public void run(){
                Looper.prepare();
                GlobalVars.table_def.initialize();
                vmessage = "erstelle Tabellen";
                handler.post(verlauf_message);
                builded = sqldriver.create_tables();
                vmessage = "setze Systemkonstanten";
                handler.post(verlauf_message);
                builded = setsystemvars(ctx);
                vmessage = "synchronisiere Einstellungen";
                handler.post(verlauf_message);
                builded = settings.sync_ini();
                builded = settings.set_ini();
                GlobalVars.system_initialized = builded;
                 switch(GlobalVars.init_flags.FLAG){
                    case 0:

                        break;
                    case GlobalVars.init_flags.UPDATE: 
                        //load the update
                        break;
                 }
                verlauf.dismiss();  
             }
        }.start();
    }

I have a similar code at the start of my application i load the current settings from the server, it works for me with:

public static ProgressDialog verlauf;
public static String vmessage = "";
static Handler handler = new Handler();;

public static void initialize_system(final Context ctx)
    {
        verlauf = ProgressDialog.show(ctx, "Starte FISforAndroid..", "synchronisiere Einstellungen",true,false);
        new Thread(){
             @Override
            public void run(){
                Looper.prepare();
                GlobalVars.table_def.initialize();
                vmessage = "erstelle Tabellen";
                handler.post(verlauf_message);
                builded = sqldriver.create_tables();
                vmessage = "setze Systemkonstanten";
                handler.post(verlauf_message);
                builded = setsystemvars(ctx);
                vmessage = "synchronisiere Einstellungen";
                handler.post(verlauf_message);
                builded = settings.sync_ini();
                builded = settings.set_ini();
                GlobalVars.system_initialized = builded;
                 switch(GlobalVars.init_flags.FLAG){
                    case 0:

                        break;
                    case GlobalVars.init_flags.UPDATE: 
                        //load the update
                        break;
                 }
                verlauf.dismiss();  
             }
        }.start();
    }
沉溺在你眼里的海 2024-11-05 18:55:55

您需要调用 showMe.execute(),而不是直接调用 doInBackgroundonPreExecute 等。

You need to call showMe.execute(), rather than directly calling doInBackground or onPreExecute etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文