Android断点下载和自动安装的示例代码

今天说一下Android中下载App到手机中并自动安装,啥也不说了先上效果图了!

创新互联建站专业为企业提供雄县网站建设、雄县做网站、雄县网站设计、雄县网站制作等企业网站建设、网页设计与制作、雄县企业网站模板建站服务,十载雄县做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

Android 断点下载和自动安装的示例代码

Android 断点下载和自动安装的示例代码

上面呢是下载中的一个图片和下载后会自动提示你安装的一个图片,二话不说,这接开代码吧!

首先来一个下布局:

<?xml version="1.0" encoding="utf-8"?> 
 
   
   
  

布局随便写了,只要是你想要的布局

然后我么来一个接口,用来帮助我么将要写的下载工具类传输数据的:

package Download; 
public interface DownloadListener { 
  void startDownload();  
  void stopDownload();  
  void finishDownload();  
  void downloadProgress(long progress); 
} 

这个接口写了4个接口方法,分别是开始下载、停止下载、完成下载以及下载是的进度。

接下来就是写下载工具类了,下载呢就使用OkHttp进行请求网络数据了,这里把这个工具类写成单利模式,方便使用!

package Download; 
import android.text.TextUtils; 
import android.util.Log; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.RandomAccessFile; 
import okhttp3.Call; 
import okhttp3.Callback; 
import okhttp3.OkHttpClient; 
import okhttp3.Request; 
import okhttp3.Response; 
import okhttp3.ResponseBody; 
public class DownloadUtils { 
  private static volatile DownloadUtils instance; 
  private final OkHttpClient client; 
  private DownloadListener mlistener; 
  private File file; 
  private String fileAbsolutePath; 
  public File downloadFile; 
  private long startPosition; 
  private Call call; 
 
  public DownloadUtils() { 
    client = new OkHttpClient(); 
  } 
  public void setListener(DownloadListener listener) { 
    this.mlistener = listener; 
  } 
 
  /** 
   * 初始化下载父路径 
   * @return 
   */ 
  public void initDownload(String path) { 
    file = new File(path); 
    if(!file.getParentFile().exists()) { 
      file.getParentFile().mkdir(); 
    } 
    if(!file.exists()) { 
      try { 
        file.createNewFile(); 
      } catch (IOException e) { 
        e.printStackTrace(); 
      } 
    } 
    fileAbsolutePath = file.getAbsolutePath(); 
    Log.d("zzz",fileAbsolutePath.toString()); 
  } 
  public static DownloadUtils getInstance() { 
    if(instance == null) { 
      synchronized (DownloadUtils.class) { 
        if(instance == null) { 
          instance = new DownloadUtils(); 
        } 
      } 
    } 
      return instance; 
  } 
  public void startDownload(String url) { 
    if(TextUtils.isEmpty(url)) { 
      return ; 
    } 
    if(url.contains(".")) { 
      String typename = url.substring(url.lastIndexOf(".") + 1); 
      if(url.contains("/")) { 
        String filename = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf(".")); 
        String fn = filename+"."+typename; 
        downloadFile = new File(this.file, fn); 
        Log.d("zzz","downloadFile"+downloadFile.toString()); 
      } 
    } 
    startPosition = 0; 
    if(downloadFile.exists()) { 
      startPosition = downloadFile.length(); 
    } 
    final Request request = new Request.Builder() 
        .addHeader("RANGE","bytes="+startPosition+"-") 
        .url(url) 
        .build(); 
    call = client.newCall(request); 
    call.enqueue(new Callback() { 
      @Override 
      public void onFailure(Call call, IOException e) { 
 
      } 
      @Override 
      public void onResponse(Call call, Response response) throws IOException { 
        mlistener.startDownload(); 
        ResponseBody body = response.body(); 
//        startPosition 
        long totalLength = body.contentLength() ; 
        Log.d("zzz", "totalLength: " + totalLength + "----"); 
        InputStream is = body.byteStream(); 
        byte[] bytes = new byte[2048]; 
        int len = 0; 
        long totalNum = startPosition; 
        RandomAccessFile raf = new RandomAccessFile(downloadFile, "rw"); 
        while ((len = is.read(bytes,0,bytes.length)) != -1) { 
          raf.seek(totalNum); 
          raf.write(bytes,0,len); 
          totalNum +=len; 
          mlistener.downloadProgress(totalNum * 100 / totalLength); 
 
        } 
         mlistener.finishDownload(); 
         body.close(); 
      } 
    }); 
  } 
  public void stopDownload() { 
    mlistener.startDownload(); 
    if(call != null && call.isExecuted()) { 
      call.cancel(); 
    } 
  } 
} 

这里做断点下载是使用了RandomAccessFile,大家可以网上去了解一下RandomAccessFile的作用。

下面是主界面的功能实现和调用,基本就是些获取控件和调用刚才写好的工具类:

package zhangtao.bwie.com.continutransform;  
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import java.io.File; 
import Download.DownloadListener; 
import Download.DownloadUtils;  
public class MainActivity extends AppCompatActivity implements View.OnClickListener{  
  private TextView pro_text; 
  private Button start_btn; 
  private Button stop_btn; 
  private String downloadUrl = "http://d.988wan.com/zft/qmzft32_988wan_01.apk"; 
  private String path = "/ZhangTao/"; 
  private ProgressBar pro_bar;  
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    initView(); 
    setOnClick(); 
    if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 
      File storageDirectory = Environment.getExternalStorageDirectory(); 
      final String absolutePath = storageDirectory.getAbsolutePath(); 
      path = absolutePath + path; 
      DownloadUtils.getInstance().initDownload(path); 
      DownloadUtils.getInstance().setListener(new DownloadListener() { 
        @Override 
        public void startDownload() { 
 
        } 
        @Override 
        public void stopDownload() { 
        } 
        @Override 
        public void finishDownload() { 
          File downloadFile = DownloadUtils.getInstance().downloadFile; 
          installApk(downloadFile); 
        } 
        @Override 
        public void downloadProgress(final long progress) { 
          runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
              pro_bar.setProgress((int) progress); 
              pro_text.setText(progress+"%"); 
            } 
          }); 
        } 
      }); 
    } 
  } 
 
  private void initView() { 
    pro_text = (TextView) findViewById(R.id.pro_text); 
    start_btn = (Button) findViewById(R.id.start_btn); 
    stop_btn = (Button) findViewById(R.id.stop_btn); 
    pro_bar = (ProgressBar) findViewById(R.id.progress); 
  } 
  private void setOnClick() { 
    start_btn.setOnClickListener(this); 
    stop_btn.setOnClickListener(this); 
  } 
  @Override 
  public void onClick(View view) { 
    switch (view.getId()) { 
      case R.id.start_btn: 
        DownloadUtils.getInstance().startDownload(downloadUrl); 
        break; 
      case R.id.stop_btn: 
        DownloadUtils.getInstance().stopDownload(); 
        break; 
    } 
  } 
  /** 
   * 安装apk 
   * @param file 
   */ 
  private void installApk(File file) { 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_VIEW); 
    intent.addCategory("android.intent.category.DEFAULT"); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); 
    startActivity(intent); 
    android.os.Process.killProcess(android.os.Process.myPid()); 
  } 
} 

上面的自动安装是installApk这个方法,这个没必要去了解太多,都是Android的一个固定方法,一般网上都会有的,希望可以帮到大家!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


分享题目:Android断点下载和自动安装的示例代码
转载源于:http://pwwzsj.com/article/gcpigh.html