应用程序查询服务器时需要显示加载屏幕
在此应用程序中,用户登录并根据服务器检查其凭据。
用户可能需要等待几秒钟,具体取决于手机打开数据连接的速度(如果有的话)。我需要在用户单击登录后显示“请稍候”或“验证凭据”或很长的内容的对话框。
所需的视觉顺序:按登录 -> 。在同一活动中显示“请稍候”对话框 ->当结果从服务器传入时,将加载新活动(或引发错误)
当前视觉顺序:按登录 ->用户等待,就像应用程序被冻结一样 ->新活动已加载
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要手动调用 AsyncTask 的
onPreExecute
/doInBackground
方法;只需对其调用execute()
,它就会从正确的线程中的正确位置调用所有方法。它违背了异步任务从 UI 线程同步调用其所有方法的全部目的(这就是您的示例代码所做的)。Don't call the
onPreExecute
/doInBackground
methods of an AsyncTask manually; just callexecute()
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).这不是使用
AsyncTask
的方式,请查看 文档。创建任务的新实例后,只需调用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 callexecute()
, not the individual methods:我在应用程序开始时有类似的代码,我从服务器加载当前设置,它适用于我:
I have a similar code at the start of my application i load the current settings from the server, it works for me with:
您需要调用
showMe.execute()
,而不是直接调用doInBackground
或onPreExecute
等。You need to call
showMe.execute()
, rather than directly callingdoInBackground
oronPreExecute
etc.