0

我有一个功能,可以更新手机上的联系人列表。当联系人更新时,我需要放置一个进度条。我需要在更新功能中添加一个进度条。我怎么做?更新功能运行良好,我只需要一个进度条来显示它正在更新。请帮忙。

 public void onClick(View v){

                //reset progress bar status
                progressBarStatus = 0;

                //reset filesize
                fileSize = 0;

                new Thread(new Runnable() {
                    public void run() {
                        while (progressBarStatus < 100) {

                            // process some tasks
                            progressBarStatus = UpdateContact();

                            // your computer is too fast, sleep 1 second
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                            // Update the progress bar
                            progressBarHandler.post(new Runnable() {
                                public void run() {
                                    progressBar.setProgress(progressBarStatus);
                                }
                            });
                        }

                        // ok, file is downloaded,
                        if (progressBarStatus >= 100) {

                            // sleep 2 seconds, so that you can see the 100%
                            try {
                                Thread.sleep(2000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }

                            // close the progress bar dialog
                            progressBar.dismiss();
                        }
                    }


                }).start();

            }

        });
4

2 回答 2

0

用这个:

这些函数将在 4 个图像之间不断改变加载,只需在更新联系人之前调用它,并在获取数据后调用 StopProgress()

XML 布局“progress_layout”:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@null"
    android:layout_gravity="center" >

    <ImageView
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/loading"
        />

</RelativeLayout>

创建 2 个方法:StartProgress() 和 StopProgress()

private Dialog  progressDialog;
    int AdsIndex, AdsCount;
    Timer timer= new Timer();
    public void startProgress()
    {
        progressDialog = new Dialog(getActivity(), android.R.style.Theme_Translucent_NoTitleBar);
        progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        progressDialog.setCancelable(false);
        progressDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        progressDialog.setContentView(R.layout.progress_layout);

        final ImageView img = (ImageView) progressDialog.findViewById(R.id.progressbar);
        final Integer drawableList[] = {R.drawable.loading, R.drawable.loading2, R.drawable.loading3, R.drawable.loading4};

        final Handler adsHandler = new Handler();    
         final Runnable adsUpdateResults = new Runnable() {
            public void run() {

                if(AdsCount > 0)
                {
                    img.setImageDrawable(getResources().getDrawable(drawableList[AdsIndex]) );

                     AdsIndex++;

                     if(AdsCount == AdsIndex)
                         AdsIndex = 0;
                }
            }
        };
        final int adsDelay = 200; // delay for xx sec.      
        final long adsPeriod = 200;//repet every xx seconds       

        timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                adsHandler.post(adsUpdateResults);
            }

        }, adsDelay, adsPeriod);

        progressDialog.show();
    }

    public void stopProgress()
    {
        progressDialog.dismiss();
    }
于 2013-08-02T12:26:55.390 回答
0

我认为带有 an 的解决方案AsyncTask会更优雅。这是的示例代码AsyncTask

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

public class MyAsyncTask<Params, Progress, Result> extends
    AsyncTask<Params, Progress, Result> {

    private final String DIALOG_MESSAGE = "Updating contacts";

    private ProgressDialog mDialog = null;

    private void setDialog(Context context) {
        this.mDialog = new ProgressDialog(context);
        this.mDialog.setMessage(DIALOG_MESSAGE);
        this.mDialog.setCancelable(false);
    }

    public MyAsyncTask(Context context) {
        this.setDialog(context);
    }

    @Override
    protected void onPreExecute() {
        this.mDialog.show();
    }

    @Override
    protected Result doInBackground(Params... arg0) {
        // Place your background executed method here
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Result result) {
        // Update the UI if u need to

        // And then dismiss the dialog
        if (this.mDialog.isShowing()) {
            this.mDialog.dismiss();
        }
    }
}

这是调用异步的代码:

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private Context context = null;

    private Button btn = null;

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

        this.context = this;

        this.btn = (Button) this.findViewById(R.id.button1);
        this.btn.setOnClickListener(onClickUpdateContacts);

    }

    private OnClickListener onClickUpdateContacts = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            MyAsyncTask<Void, Void, Void> updateTask = new MyAsyncTask<Void, Void, Void>(context);
            updateTask.execute();
        }

    };

}

You can also easily change the look of the dialog to match the look&feel of your app. Hope this simple code helps ^^

于 2013-08-02T13:15:43.627 回答