0

我想在网格视图中加载图像时显示进度对话框。
我面临的问题是当前线程和进度对话框线程同时运行。

 public String Method1(){
        String output="";
        final ProgressDialog aProgDialogL = ProgressDialog.show(this, "", "Loading...");
        Thread thread = new Thread() {
            public void run () {
                //My codes
                aHandlerL.post(new Runnable() {
                    @Override
                    public void run() {
                        //Post Runnable codes
                        aProgDialogL.dismiss();
                    }
                });
            }
        };
        thread.start();

        /*
         * 
         * 
         * OTHER CODES
         * 
         * 
         */
        return output;
 }  

在上面的示例中,我需要在 Progress Dialog Thread 中运行代码。完成执行后,我需要运行我的“其他代码”。怎么做?
.
我尝试使用异步任务。在异步任务完成之前,method1 被执行并重新使用字符串。

    public String Method1(){
    String result="";
    new GetImages().execute();
    return result;

}
public class GetData extends AsyncTask<Void, Void, Integer>{

    @Override
    protected void onPreExecute() {
        aProgDialogL = ProgressDialog.show(Main.this, "", "Loading...");
    }

    @Override
    protected Integer doInBackground(Void... params) {
        //Progress Dialig Code
        return null;
    }

    @Override
    protected void onPostExecute(Integer result) {
        aProgDialogL.dismiss();
        //OTHER CODES
        super.onPostExecute(result);
    }

}
4

4 回答 4

1

您可以使用异步任务。http://developer.android.com/reference/android/os/AsyncTask.html。这里有一个很好的教程。http://www.vogella.com/articles/AndroidPerformance/article.html。也看看这个链接 http://developer.android.com/guide/components/processes-and-threads.html。使用 asynctask 根据您的需要对其进行修改。

     doInBackground()- For long running operations. Don't update ui here.
     onPreExecute()- update ui before running the operatio nin background.
     onPostExecute()- update ui after running the operation.
于 2012-11-23T09:01:30.060 回答
1

我建议你采取一些不同的方法。

不要在 Method1() 函数中涉及任何线程。相反,您的 Method1() 函数应该在单独的线程下运行。

下面的片段会帮助你。

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((Button) findViewById(R.id.btnPopup))
            .setOnClickListener(new OnClickListener() {


            public void onClick(View v) {

                new Thread() {
                    public void run() {

                        String answer = Method1();

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                // Here you will write the code which is
                                // to be executed on main thread.

                            }
                        });
                    };
                }.start();
            }
        });

    }

    public String Method1() {
        // Write code

        return "result";
    }
}
于 2012-11-23T09:15:15.937 回答
0

我建议使用 AsyncTask 来解决你的问题

http://p-xr.com/merging-2-tutorials-xml-data-progressdialog-using-asynctask/

http://p-xr.com/android-tutorial-how-to-make-a-progress-dialog/

http://developer.android.com/reference/android/os/AsyncTask.html
于 2012-11-23T08:59:48.887 回答
0

取而代之的是:在特定时间之后使用 Timer 停止你的线程并在停止语句之后编写你想要的代码就像这样:

Timer timer=new Timer();
 timer.schedule(new TimerTask() {
 @Override
public void run() {
     thread.stop;
       // Other code   }

},你想要的时间);

于 2012-11-23T09:34:27.850 回答