onPostExecute()等待太久了?

时间:2012-12-12 10:01:26

标签: android user-interface

我有一些控制,因此我做了一些线程。我想用asycntask显示加载栏一个屏幕到另一个屏幕。我的问题是加载栏快速显示和消失,然后等待相同的屏幕很多而不加载bar.And一段时间后它将进入第二个屏幕。我认为post post对我来说没有用。我可以帮助我吗?

这是我的代码:

private class ProgressBarAsync extends AsyncTask<Void, Integer, Void>{

        /** This callback method is invoked, before starting the background process */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            /** Creating a progress dialog window */
            mProgressDialog = new ProgressDialog(Login.this);

            /** Close the dialog window on pressing back button */
            mProgressDialog.setCancelable(true);

            /** Setting a horizontal style progress bar */
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

            /** Setting a message for this progress dialog
             * Use the method setTitle(), for setting a title
             * for the dialog window
             *  */
            mProgressDialog = ProgressDialog.show(Login.this, 
                    "Giriş yapıyorsunuz", "Lütfen bekleyin...");



        }

        /** This callback method is invoked on calling execute() method
         * on an instance of this class */
        @Override
        protected Void doInBackground(Void...params) {

            try {
                startMyApplication() ;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        /** This callback method is invoked when publishProgress()
         * method is called */
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            mProgressDialog.setProgress(mProgressStatus);
        }

        /** This callback method is invoked when the background function
         * doInBackground() is executed completely */
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if (mid > 0) {
                Intent btn_login = new Intent(
                        getApplicationContext(), MainScreen.class);
                startActivity(btn_login);
            }
            else {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(Login.this);

                // Setting Dialog Title
                alertDialog.setTitle("GİRİŞ");

                // Setting Dialog Message
                alertDialog.setMessage("Kullanıcı adı veya Parola Hatalı Lütfen tekrar deneyin.");

                // Setting Icon to Dialog
                //alertDialog.setIcon(R.drawable.delete);

                // Setting Negative "NO" Button
                alertDialog.setNegativeButton("TAMAM", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.cancel();
                    }
                });

                // Showing Alert Message
                alertDialog.show();         
            }

            //run();                    
            mProgressDialog.dismiss();

这是startmyapp theads:

public void startMyApplication() throws InterruptedException {

    ExecutorService executor = Executors.newFixedThreadPool(2);
    FutureTask<String> futureOne = new FutureTask<String>(
            new Callable<String>() {
                public String call() throws Exception {
                    if (isOnline()) {
                        // Call Login Web Service with username and password and get mid
                        mid = callLoginWS(username.getText().toString(), password.getText().toString());                            
                        if (mid > 0) {
                            //callPasswordGeneratorWS(mid);
                            // Insert mid and username into sqllite
                            dbInstance.insertMerch(mid,username.getText().toString());
                        }
                        Log.i("futureone", "futureone");

                    }
                    return "TEST";  
                }
            });

    FutureTask<String> futureTwo = new FutureTask<String>(
            new Callable<String>() {
                public String call() throws Exception {
                    // Get mid from database
                    mid = dbInstance.selectMerch(username.getText().toString());
                    return "TEST";
                }

            });



    // ... Dispatch
    // Check if user exists previously
    // ... Dispatch     
    mid = dbInstance.selectMerch(username.getText().toString());
    dbInstance.close();


    executor.execute(futureOne);
    while (!(futureOne.isDone())) {
        executor.execute(futureTwo);
    }
    Log.i("login","new login");
    //}
    executor.shutdown();
}

3 个答案:

答案 0 :(得分:3)

此部分导致延迟。

while (!(futureOne.isDone())) {
        executor.execute(futureTwo);
    }

在此处,您将阻止当前线程完成。并等待futureOne完成。

答案 1 :(得分:0)

首先,尝试在没有 ExecutorService 的情况下执行 startMyApplication 方法。当您在 doInBackground 中运行代码时,您已经处于不同的线程中(如果我错了,请更正我),因此在不使用额外线程的情况下运行代码。这样做并检查一切是否正常。

另外,我注意到你没有在 doInBackground 方法的任何地方调用 publishProgress(Progress ... values)方法。因此,您无法为进度条发布正确的值。

答案 2 :(得分:0)

如果我没错。您已经使用AsyncTask异步执行startMyApplication(),因此为什么要创建FutureTask。您已经处于后台任务中。 (考虑到中期很长) 尝试修改这样的代码,

    public Long startMyApplication(){
       if (isOnline()) {
          // Call Login Web Service with username and password and get mid
          mid = callLoginWS(username.getText().toString(), password.getText().toString());                            
          if (mid > 0) {
                //callPasswordGeneratorWS(mid);
                // Insert mid and username into sqllite
                dbInstance.insertMerch(mid,username.getText().toString());
          }
          Log.i("futureone", "futureone");

          }
      mid = dbInstance.selectMerch(username.getText().toString()); //OR directly returning mid without fetching from db as it will be same.
      return mid;
    }

现在在doInBackground。

@Override
protected Long doInBackground(Void...params) {
            Long temp = -1l;
            try {
                temp = startMyApplication() ;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return temp;
 }

@Override
protected void onPostExecute(Long mid) {
mProgressDialog.dismiss(); //First dismiss the progress bar.
//Rest of ur code will go same in this. Remember mid is now accessible correctly.
}

更改以下行

private class ProgressBarAsync extends AsyncTask<Void, Integer, Void>

通过

private class ProgressBarAsync extends AsyncTask<Void, Integer, Long>

据我所知,这可能对你有用。