2011-04-05 49 views
3

在这个应用程序,用户登录和他们的凭据检查服务器。需要显示加载屏幕,而应用程序查询服务器

用户可能会等待几秒钟,具体取决于手机可以打开数据连接的速度。我需要对话框说“请稍候”或“验证凭据”或用户点击登录后长这些行的东西。

所需的视觉顺序:按下登录 - >“请稍候”对话框显示在此相同活动 - >时,结果从服务器提供新的活动被加载(或引发错误)

当前可视顺序:按日志 - >用户等待的,如果应用程序被冻结 - >新活动被载入

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

回答

2

这不是你如何使用AsyncTask,看看documentation。一旦你创建你的任务的新实例,只需拨打​​,而不是单独的方法:

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

谢谢,这个澄清真的为我做了! – CQM 2011-04-08 17:47:45

1

我在我的应用程序加载来自服务器的当前设置的开始类似的代码,它为我:

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(); 
    } 
3

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

+0

打我给它: ) – 2011-04-05 15:16:05

0

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

相关问题