HttpClient更新下载apk

实现功能:

1、提示是否更新

2、下载完成后,提示安装

3、安装完成后,提示打开

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by dugaolong on 16/5/21.
 */
public class DownloadApk extends Activity {
    private static final String TAG = "DownloadTest";
    private String getUrl = "http://192.168.1.106:8080/http/app-debug.apk";
    private ProgressDialog pd;


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

    /**
     * 绑定button的事件
     * @param view
     */
    public void down(View view){
        Dialog dialog =new AlertDialog.Builder(this)
                .setTitle("版本更新")
                .setMessage("当前版本:1.2\r\n最新版本:1.3")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        download();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .create();
        dialog.show();
    }

    /**
     * 下载apk
     */
    public void download() {
        pd = new ProgressDialog(this);
        pd.setMessage("正在更新,请稍后。。。");
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.show();
        //连接网络下载,新线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(getUrl);
                try {
                    HttpResponse response = httpClient.execute(httpGet);
                    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                        //判断sd卡是否安装
                        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                            //下载apk存放位置
                            File file = new File(Environment.getExternalStorageDirectory(), "aaa.apk");//文件路径
                            FileOutputStream fos = new FileOutputStream(file);//创建文件的输出流
                            InputStream is = response.getEntity().getContent();//服务器返回的流
                            BufferedInputStream bis = new BufferedInputStream(is);
                            //apk总大小
                            int total =(int)response.getEntity().getContentLength();
                            pd.setMax(total);
                            //写入文件
                            byte[] buffer = new byte[1024];//一次读取1024字节
                            int len;
                            int pro = 0;
                            while ((len = bis.read(buffer)) != -1) {
                                fos.write(buffer, 0, len);
                                pro += len;
                                pd.setProgress(pro);
                            }
                            fos.close();
                            bis.close();
                            is.close();
                            pd.dismiss();
                            //安装apk
                            installApk(file);

                        }
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    private void installApk(File file) {
        Intent intent = new Intent();
        //执行显示的动作
        intent.setAction(Intent.ACTION_VIEW);
        //在新的任务栈中启动activity(添加这句话以后,会提示用户打开或者完成)
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        //执行的数据类型MIME类型
        //apk的MIME类型为:application/vnd.android.package-archive
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        startActivity(intent);


    }
}

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/tb_download"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开始下载apk"
        android:onClick="down"/>

    <ImageView
        android:id="@+id/iv_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>





在第download()方法后面添加下面的方法,可以在通知栏显示下载进度百分比。

 Uri uri = Uri.parse("http://www.an12.com/download.php?app_id=1795&id=148532");
                        Intent intent = new Intent(Intent.ACTION_VIEW,uri);
                        startActivity(intent);
效果如下:



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值