zoukankan      html  css  js  c++  java
  • HttpUtils实现断点下载APP并实现弹出安装窗口

    package com.example.downloadapk;

    import java.io.File;

    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.lidroid.xutils.HttpUtils;
    import com.lidroid.xutils.exception.HttpException;
    import com.lidroid.xutils.http.ResponseInfo;
    import com.lidroid.xutils.http.callback.RequestCallBack;

    public class MainActivity extends Activity {

        private ProgressBar bar;
        private TextView time;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button button = (Button) findViewById(R.id.download);
            bar = (ProgressBar) findViewById(R.id.progressBar1);
            time = (TextView) findViewById(R.id.time);
            // 按钮的点击监听
            button.setOnClickListener(new OnClickListener() {

                private String path;

                public void onClick(View v) {
                    HttpUtils utils = new HttpUtils();

                    if (Environment.getExternalStorageState().equals(
                            Environment.MEDIA_MOUNTED)) {
                        path = Environment.getExternalStorageDirectory().getPath();
                    }

                    utils.download(
                            "http://101.200.142.201:8080/tqyb/pxshipeidemo.apk",
                            path + "/4a.avi", true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
                            true, new RequestCallBack<File>() {

                                @Override
                                public void onStart() {

                                    Toast.makeText(MainActivity.this, "开始下载……", 0)
                                            .show();
                                    super.onStart();
                                }

                                @Override
                                public void onLoading(long total, long current,
                                        boolean isUploading) {
                                    bar.setMax((int) total);
                                    Toast.makeText(MainActivity.this, "正在下载……", 0)
                                            .show();
                                    int a = (int) current;
                                    bar.setProgress(a);
                                    time.setText(current * 100 / total + "%");
                                    super.onLoading(total, current, isUploading);
                                }

                                @Override
                                public void onSuccess(ResponseInfo<File> arg0) {
                                    Toast.makeText(getApplicationContext(),
                                            arg0.result.getPath(), 0).show();

                                    String fileName = path + "/4a.avi";
                                    Intent i = new Intent();
                                    i.setAction(Intent.ACTION_VIEW);
                                    i.setDataAndType(
                                            Uri.fromFile(new File(fileName)),
                                            "application/vnd.android.package-archive");
                                    startActivity(i);

                                }

                                public void onFailure(HttpException arg0,
                                        String arg1) {
                                    Toast.makeText(MainActivity.this,
                                            "下载失败,请检查网络……", 0).show();
                                }

                            });
                }
            });
        }

    }

  • 相关阅读:
    Codeforces 451A Game With Sticks
    POJ 3624 Charm Bracelet
    POJ 2127 Greatest Common Increasing Subsequence
    POJ 1458 Common Subsequence
    HDU 1087 Super Jumping! Jumping! Jumping!
    HDU 1698
    HDU 1754
    POJ 1724
    POJ 1201
    CSUOJ 1256
  • 原文地址:https://www.cnblogs.com/weiyangge/p/5436449.html
Copyright © 2011-2022 走看看