进度栏介于两项活动之间

时间:2013-01-17 10:26:33

标签: android android-progressbar

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

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

5 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

  1. doInBackground() - 在此处实施您的长期任务
  2. onPostExecute() - 转到第二项活动

答案 3 :(得分:0)

看看这个示例,这将清除所有与概念相关的进度条和Asynctask

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

答案 4 :(得分:0)

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