android下载apk并安装1、设置权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
2、业务代码
ample.esri.app04work;
import android.app.ProgressDialog;
t.Intent;
import android.Uri;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.HttpURLConnection;
import java.URI;
import java.URL;
public class MainActivity extends AppCompatActivity {
private File apkFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
}
public void downloadAPK(View v){
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.show();
//准备⽤于保存apk⽂件的file对象放在sd卡⾥⾯
apkFile = new File(getExternalFilesDir(null), "update.apk");
//启动分线程下载数据,显⽰下载进度
new Thread(new Runnable() {
@Override
public void run() {
try{
//1 得到链接对象
String path = "192.168.0.184:8080/storage.apk";
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(10000);
int responseCode = ResponseCode();
if(responseCode == 200){
//设置dialog最⼤进度
dialog.ContentLength());
InputStream is = InputStream();
FileOutputStream fos = new FileOutputStream(apkFile);
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1){
fos.write(buffer, 0, len);
dialog.incrementProgressBy(len);
}
fos.close();下载安装
is.close();
}
connection.disconnect();
//主线程,移除dialog,启动安装
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();
installAPK();
}
});
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
}
/**
* 启动安装apk
*/
private void installAPK() {
Intent intent = new Intent("android.intent.action.INSTALL_PACKAGE");
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); startActivity(intent);
}
}
使⽤异步任务下载
ample.esri.l05.handler;
import android.app.ProgressDialog;
t.Intent;
import android.Uri;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.HttpURLConnection;
import java.URL;
public class AsyncTaskActivity extends AppCompatActivity {
private File apkFile;
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_async_task);
}
public void downloadAPK(View v){
//启动异步任务处理
new AsyncTask<Void, Integer, Void>() {
//1主线程显⽰提⽰试图
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(AsyncTaskActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.show();
apkFile = new File(getExternalFilesDir(null), "update.apk");
}
//2分线程,联⽹请求
@Override
protected Void params) {
try{
//1 得到链接对象
String path = "192.168.0.184:8080/storage.apk";
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000);
connection.setReadTimeout(10000);
int responseCode = ResponseCode();
if(responseCode == 200){
//设置dialog最⼤进度
dialog.ContentLength());
InputStream is = InputStream();
FileOutputStream fos = new FileOutputStream(apkFile);
byte[] buffer = new byte[1024];
int len = -1;
while ((len = is.read(buffer)) != -1){
fos.write(buffer, 0, len);
//在分线程发布进度
publishProgress(len);
// dialog.incrementProgressBy(len);
}
fos.close();
is.close();
}
connection.disconnect();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
//3主线程,更新界⾯
@Override
protected void onPostExecute(Void aVoid) {
dialog.dismiss();
installAPK();
}
//在主线程中更新进度(在publishProgress之后执⾏)
@Override
protected void values) {
dialog.incrementProgressBy(values[0]);
}
}.execute();
}
/**
* 启动安装apk
*/
private void installAPK() {
Intent intent = new Intent("android.intent.action.INSTALL_PACKAGE");
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); startActivity(intent);
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论