在此应用程序中,用户登录并根据服务器检查他们的凭据。
用户可以等待几秒钟,这取决于电话打开数据连接的速度(如果有的话)。我需要对话框说“请等待”或“验证凭证”或用户点击登录后,这些行很长的东西。
所需的可视化顺序:当结果来自服务器时,在->中按下日志“请等待”对话框将在此相同的活动->中显示一个新的活动被加载(或抛出错误)
当前可视化顺序:按log in ->用户等待,就像应用程序被冻结一样加载新活动->
我正试着用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) {发布于 2011-04-05 23:14:02
这不是你使用AsyncTask的方式,看看documentation吧。一旦创建了任务的新实例,只需调用execute(),而不是单个方法:
Progressor showMe = new Progressor();
showMe.execute();发布于 2011-04-05 23:13:43
不要手动调用AsyncTask的onPreExecute/doInBackground方法;只需对其调用execute(),它就会从正确的线程在适当的位置调用您的所有方法。它违背了异步任务从UI线程同步调用其所有方法的全部目的(这就是您的示例代码所做的)。
发布于 2011-04-05 23:10:32
我有一个类似的代码,在我的应用程序开始时,我从服务器加载当前设置,它适用于我:
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();
}https://stackoverflow.com/questions/5554077
复制相似问题