使用afinal框架实现简单的下载

来源:互联网 发布:网络诈骗主题班会记录 编辑:程序博客网 时间:2024/05/22 21:33
这是一只小菜鸟的日常笔记,希望得到大神指点........






public class MainActivity extends Activity implements OnClickListener {

 private Button mBtn_Start;
 private Button mBtn_Pause;
 private ProgressBar mProgressBar;
 private TextView mDTextView;
 private HttpHandler<File> handler;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  initView();

 }

 /**
  * 初始化控件
  */
 private void initView() {

  mBtn_Start = (Button) findViewById(R.id.btn_start);
  mBtn_Pause = (Button) findViewById(R.id.btn_pause);
  mDTextView = (TextView) findViewById(R.id.tv_download);
  mProgressBar = (ProgressBar) findViewById(R.id.pb_progressbar);

  mBtn_Start.setOnClickListener(this);
  mBtn_Pause.setOnClickListener(this);

 }

 @Override
 public void onClick(View v) {

  switch (v.getId()) {
  case R.id.btn_start:
   Toast.makeText(this, "开始下载", Toast.LENGTH_SHORT).show();

   // 请求网络,下载文件
   downLoad();

   break;
  case R.id.btn_pause:

   Toast.makeText(this, "暂停", Toast.LENGTH_SHORT).show();

   // 调用stop()方法暂停下载
   handler.stop();
   break;

  }

 }

 /**
  * 下载文件
  */
 private void downLoad() {

  FinalHttp finalHttp = new FinalHttp();
  // 下载路径
  String url = "http://192.168.101.144:8080/360MobileSaf.apk";
  // 保存到本地的路径
  String target = "/mnt/sdcard/MobileSaf.apk";

  handler = finalHttp.download(url, target, true,
    new AjaxCallBack<File>() {

     @Override
     public int getRate() {
      return super.getRate();
     }

     @Override
     public void onLoading(long count, long current) {
      super.onLoading(count, current);
      System.out.println("开始下载");
      mProgressBar.setVisibility(View.VISIBLE);
      mProgressBar.setProgress((int) current);
      mProgressBar.setMax((int) count);

      mDTextView.setVisibility(View.VISIBLE);

      // 把long转化为MB
      String progress = Formatter.formatFileSize(
        MainActivity.this, current);
      String total = Formatter.formatFileSize(
        MainActivity.this, count);

      mDTextView.setText("已下载:" + progress + "总共:" + total);
      // mDTextView.setText("速度"+getRate());
     }

     @Override
     public void onSuccess(File t) {
      super.onSuccess(t);
      System.out.println("下载完成");
      Toast.makeText(MainActivity.this, "下载完成",
        Toast.LENGTH_SHORT).show();
      mProgressBar.setVisibility(View.GONE);
      mDTextView.setVisibility(View.GONE);

     }

     @Override
     public void onFailure(Throwable t, String strMsg) {
      super.onFailure(t, strMsg);
      Toast.makeText(MainActivity.this, "下载失败",
        Toast.LENGTH_SHORT).show();
     }

    });

 }

}



下面贴出布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_start"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="开始下载" />

        <Button
            android:id="@+id/btn_pause"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="暂停下载" />
    </LinearLayout>

    <ProgressBar
        android:id="@+id/pb_progressbar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:visibility="gone" />

    <TextView
        android:id="@+id/tv_download"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="已下载xxM,总共xxM"
        android:visibility="gone" />

</LinearLayout>






0 0
原创粉丝点击