未命名   

程式碼下載

 

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.smart.impl.SmartDownloadProgressListener;
import com.smart.impl.SmartFileDownloader;

public class SmartDownloadActivity extends Activity {
    private ProgressBar downloadbar;
    private EditText pathText;
    private TextView resultView;
    private Handler handler = new Handler(){

  @Override//陓洘
  public void handleMessage(Message msg) {
   switch (msg.what) {
   case 1:
    int size = msg.getData().getInt("size");
    downloadbar.setProgress(size);
    float result = (float)downloadbar.getProgress()/ (float)downloadbar.getMax();
    int p = (int)(result*100);
    resultView.setText(p+"%");
    if(downloadbar.getProgress()==downloadbar.getMax())
     Toast.makeText(SmartDownloadActivity.this, R.string.success, 1).show();
    break;

   case -1:
    Toast.makeText(SmartDownloadActivity.this, R.string.error, 1).show();
    break;
   }
   
  }     
    };
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button button = (Button)this.findViewById(R.id.button);
        downloadbar = (ProgressBar)this.findViewById(R.id.downloadbar);
        pathText = (EditText)this.findViewById(R.id.path);
        resultView = (TextView)this.findViewById(R.id.result);
        button.setOnClickListener(new View.OnClickListener() {   
   @Override
   public void onClick(View v) {
    String path = pathText.getText().toString();
    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
     File dir = Environment.getExternalStorageDirectory();
     download(path, dir);
    }else{
     Toast.makeText(SmartDownloadActivity.this, R.string.sdcarderror, 1).show();
    }
   }
  });
    }

    private void download(final String path, final File dir){
     new Thread(new Runnable() {
   @Override
   public void run() {
    try {
     SmartFileDownloader loader = new SmartFileDownloader(SmartDownloadActivity.this, path, dir, 3);
     int length = loader.getFileSize();
     downloadbar.setMax(length);
     loader.download(new SmartDownloadProgressListener(){
      @Override
      public void onDownloadSize(int size) {
       Message msg = new Message();
       msg.what = 1;
       msg.getData().putInt("size", size);       
       handler.sendMessage(msg);
      }});
    } catch (Exception e) {
     Message msg = new Message();
     msg.what = -1;
     msg.getData().putString("error", "狟婥囮啖");
     handler.sendMessage(msg);
    }
   }
  }).start();//羲宎
     
    }
}

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()