下面是我之前實作下載一個zip檔案(裡面是sqlite的檔案),解壓縮的過程,所用的方式是使用 ProgressBar ,提供你參考
WelcomeActivity.java- public class WelcomeActivity extends Activity implements Runnable {
- private static final String TAG = WelcomeActivity.class.getSimpleName();
- private final int HANDLER_OF_SUCCESS = 0;
- private final int HANDLER_OF_DOWNLOAD = 1;
- private final int HANDLER_OF_DOWNLOAD_END = 2;
- private int file_size = 0;
- private int down_size = 0;
- private Thread thread;
- private DownloadHelper db;
- private ProgressBar progressBar1;
- private TextView textView1;
- private File download_path = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.welcome);
- if (Integer.valueOf(android.os.Build.VERSION.SDK) > 7) {
- download_path = Environment.getExternalStoragePublicDirectory("/mctw");
- } else {
- download_path = new File("/sdcard/mctw");
- }
- download_path.mkdirs();
- progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
- textView1 = (TextView) findViewById(R.id.textView1);
- thread = new Thread(this);
- thread.start();
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- String download_link = "http://xx.xx.xx.xx/app/data/xxx.zip";
- File download_file = new File(download_path, "xxx.zip");
- try {
- URL download_url = new URL(download_link);
- URLConnection conn = download_url.openConnection();
- conn.connect();
- InputStream is = conn.getInputStream();
- file_size = conn.getContentLength();
- FileOutputStream fos = new FileOutputStream(download_file);
- byte buf[] = new byte[1024];
- down_size = 0;
- sendMsg(HANDLER_OF_DOWNLOAD);
- do {
- int numread = is.read(buf);
- if (numread == -1) {
- break;
- }
- fos.write(buf, 0, numread);
- down_size += numread;
- sendMsg(HANDLER_OF_DOWNLOAD);
- } while (true);
- sendMsg(HANDLER_OF_DOWNLOAD_END);
- is.close();
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- Log.e(TAG, e.getMessage());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- Log.e(TAG, e.getMessage());
- }
- }
- private void sendMsg(int flag) {
- Message msg = new Message();
- msg.what = flag;
- handler.sendMessage(msg);
- }
- private Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- Bundle bundle = msg.getData();
- switch (msg.what) {
- case HANDLER_OF_SUCCESS:
- break;
- case HANDLER_OF_DOWNLOAD:
- double x = (double) down_size;
- double y = (double) file_size;
- double person = (x / y) * 100;
- int personInt = (int) person;
- if (progressBar1 != null) {
- progressBar1.setProgress(personInt);
- textView1.setText(getString(R.string.app_download) + Integer.toString(personInt) + "%");
- }
- break;
- case HANDLER_OF_DOWNLOAD_END:
- textView1.setText(getString(R.string.app_compress));
- File download_file = new File(download_path, "xxx.zip");
- File sqlite_file = new File(download_path, "xxx.db");
- try {
- FileInputStream fin = new FileInputStream(download_file);
- ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fin));
- ZipEntry entry;
- while ((entry = zin.getNextEntry()) != null) {
- int size;
- byte[] buffer = new byte[2048];
- FileOutputStream fos = new FileOutputStream(sqlite_file);
- BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
- while ((size = zin.read(buffer, 0, buffer.length)) != -1) {
- bos.write(buffer, 0, size);
- }
- bos.flush();
- bos.close();
- }
- zin.close();
- fin.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- Log.e(TAG, e.getMessage());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- Log.e(TAG, e.getMessage());
- }
- download_file.delete();
- textView1.setText(getString(R.string.app_welcome));
- startActivity(new Intent().setClass(WelcomeActivity.this, MenuActivity.class));
- finish();
- break;
- }
- }
- };
- }
複製代碼 welcome.xml- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <FrameLayout android:id="@+id/frameLayout1" android:layout_width="match_parent" android:layout_height="match_parent">
- <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" android:orientation="vertical">
- <ProgressBar android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:id="@+id/progressBar1" android:layout_width="match_parent" android:max="100"></ProgressBar>
- <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1" android:layout_gravity="center" android:text="@string/app_download"></TextView>
- </LinearLayout>
- </FrameLayout>
- </LinearLayout>
複製代碼 |