0

我正在制作一个非常简单的程序。我只想在第一个和第二个活动之间显示一个进度条。

活动 1 只包含一个按钮,一旦我单击它,它应该在同一活动中显示一个进度条并一直显示,直到第二个活动出现,其中只包含一条正在显示的消息。

4

5 回答 5

1

每当有任何进程需要更多时间时都会使用 ProgressBar,那么我们应该使用异步任务并显示进度对话框,直到完成任务

如果您有任何类似的要求,请遵循这些

private class LongOperation extends AsyncTask<String, Void, String> 
{
    protected void onPreExecute() 
    { 
        progressDialog = new ProgressDialog(activity.this); 
        progressDialog.setTitle("Processing...");
        progressDialog.setMessage("Please wait...");
        progressDialog.setCancelable(true);
        progressDialog.show();
    }

    protected String doInBackground(String... params) 
    {
        try 
        {
            //BackGround process();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String result) 
    {

        progressDialog.dismiss();
        Intent n = new Intent(firstactivity.this, secondactivity.class);
        startActivity(n);
    }
  }

这个怎么称呼

ProgressDialog progressDialog;
LongOperation mytask = null;
mytask = new LongOperation();
mytask.execute();
于 2013-01-17T10:33:36.833 回答
0

看看这个例子,这将清除你所有与进度条和异步任务相关的概念

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class download extends Activity {

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private Button startBtn;
private ProgressDialog mProgressDialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    startBtn = (Button)findViewById(R.id.startBtn);
    startBtn.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            startDownload();
        }
    });
}

private void startDownload() {
    String url = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
    new DownloadFileAsync().execute(url);
}
@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_DOWNLOAD_PROGRESS:
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("Downloading file..");
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
        return mProgressDialog;
    default:
        return null;
    }
}

class DownloadFileAsync extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
    super.onPreExecute();
    showDialog(DIALOG_DOWNLOAD_PROGRESS);
}

@Override
protected String doInBackground(String... aurl) {
    int count;

try {

URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();

int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard    /some_photo_from_gdansk_poland.jpg");

byte data[] = new byte[1024];

long total = 0;

    while ((count = input.read(data)) != -1) {
        total += count;
        publishProgress(""+(int)((total*100)/lenghtOfFile));
        output.write(data, 0, count);
    }

    output.flush();
    output.close();
    input.close();
} catch (Exception e) {}
return null;

}
protected void onProgressUpdate(String... progress) {
     Log.d("ANDRO_ASYNC",progress[0]);
     mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String unused) {
    dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
}
于 2013-01-17T10:34:20.543 回答
0

在执行繁重的工作之前调用第二个活动并从那里显示进度条会更有效。看看 AsyncTask 类是如何工作的。

于 2013-01-17T10:30:45.830 回答
0

我假设您想在移动到第二个活动时实现长时间运行的后台任务。如果是这种情况,则实施AsyncTask

您可以实现 4 种 AsyncTask 方法,但您可以执行以下操作:

  1. doInBackground()- 在这里执行您长期运行的任务
  2. onPostExecute()- 移动到第二个活动
于 2013-01-17T10:31:06.993 回答
0

在切换到新活动之前显示 ProgressDialog,或者在显示新活动之后显示,但数据仍在加载。你可以检查它......在这里:http: //developer.android.com/reference/android/app/ProgressDialog.html

于 2014-01-26T05:52:00.540 回答