ITeye博客

progress dialog例子

Aga · 2009-09-21 14:59 · Android · Markdown

package org.cxz.research;

public interface IncreasePercentage {
	public void increase(int increment);
	public boolean isFull();
}


package org.cxz.research;

import java.util.Random;

public class FakeDownloader implements Runnable {
	IncreasePercentage mIncreaser = null;
	
	public FakeDownloader(IncreasePercentage increaser) {
		super();
		mIncreaser = increaser;
	}
	
	@Override
	public void run() {
		Random r = new Random();
		while(mIncreaser.isFull()){
			mIncreaser.increase(Math.abs(r.nextInt(10)));
			try {
				Thread.sleep(1000 * 1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}

}


package org.cxz.research;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;

public class PercentageDialog extends Activity implements IncreasePercentage{
	
	private static interface DIALOG_IDS{
		public static final int PERCENTAGE_DIALOG = 0;
	}
	
	private ProgressDialog mPercentageDialog = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        showDialog(DIALOG_IDS.PERCENTAGE_DIALOG);
    }

	@Override
	protected void onResume() {
		new Thread(new FakeDownloader(this)).start();
		super.onResume();
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch(id){
		case DIALOG_IDS.PERCENTAGE_DIALOG:
			mPercentageDialog = new ProgressDialog(this);
			mPercentageDialog.setProgress(0);
			mPercentageDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mPercentageDialog.setMessage("some msgs");
			mPercentageDialog.setMax(100);
			return mPercentageDialog;
		default:
			break;
		}
		return super.onCreateDialog(id);
	}

	@Override
	public void increase(final int increment) {
		runOnUiThread(new Runnable(){

			@Override
			public void run() {
				mPercentageDialog.setProgress(mPercentageDialog.getProgress() + increment);
				if(mPercentageDialog.getProgress() >= 100){
					mPercentageDialog.dismiss();
				}
			}
			
		});
		
	}

	@Override
	public boolean isFull() {
		return mPercentageDialog.isShowing();
	}
	
}

版本二:

package org.cxz.research;

import java.util.Observable;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class PercentageDialog extends Activity implements IncreasePercentage{
	
	private static interface DIALOG{
		public static final int PERCENTAGE_DIALOG = 0;
	}
	
	private static interface OPTION{
		public static final int START_DOWNLOADING = 10;
	}
	
	private ProgressDialog mPercentageDialog = null;
	private Observable mObservable = new Observable();
	private FakeDownloader mDownloader = new FakeDownloader(this);
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mObservable.addObserver(mDownloader);
    }

	protected void startDownload() {
		new Thread(mDownloader, "Fake Downloader").start();
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, OPTION.START_DOWNLOADING, 0, "start");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case OPTION.START_DOWNLOADING:
			showDialog(DIALOG.PERCENTAGE_DIALOG);
			mPercentageDialog.setProgress(0);
			startDownload();
		}
		return true;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch(id){
		case DIALOG.PERCENTAGE_DIALOG:
			mPercentageDialog = new ProgressDialog(this);
			mPercentageDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mPercentageDialog.setMessage("some msgs");
			mPercentageDialog.setMax(100);
			return mPercentageDialog;
		default:
			break;
		}
		return super.onCreateDialog(id);
	}

	@Override
	public void increase(final int increment) {
		runOnUiThread(new Runnable(){

			@Override
			public void run() {
				mPercentageDialog.setProgress(mPercentageDialog.getProgress() + increment);
				if(mPercentageDialog.getProgress() >= 100){
					mPercentageDialog.dismiss();
					mObservable.notifyObservers();
				}
			}
			
		});
		
	}	
}
package org.cxz.research;

public interface IncreasePercentage {
	public void increase(int increment);
}

package org.cxz.research;

import java.util.Observable;
import java.util.Observer;
import java.util.Random;

public class FakeDownloader implements Runnable, Observer{
	IncreasePercentage mIncreaser = null;
	
	private boolean isFull = false;
	
	public FakeDownloader(IncreasePercentage increaser) {
		super();
		mIncreaser = increaser;
	}
	
	@Override
	public void run() {
		Random r = new Random();
		while(!isFull){
			mIncreaser.increase(Math.abs(r.nextInt(10)));
			try {
				Thread.sleep(1000 * 1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		isFull = true;
	}
	
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。