admin管理员组

文章数量:1530517

2023年12月13日发(作者:)

android下载apk并安装

1、设置权限

2、业务代码

package k;

import ssDialog;

import ;

import ;

import Clock;

import patActivity;

import ;

import ;

import ;

import tputStream;

import tream;

import LConnection;

import ;

import ;

public class MainActivity extends AppCompatActivity {

private File apkFile;

@Override

protected void onCreate(Bundle savedInstanceState) {

te(savedInstanceState);

setContentView(ty_main);

}

public void downloadAPK(View v){

final ProgressDialog dialog = new ProgressDialog(this);

gressStyle(_HORIZONTAL);

();

//准备用于保存apk文件的file对象 放在sd卡里面

apkFile = new File(getExternalFilesDir(null), "");

//启动分线程下载数据,显示下载进度

new Thread(new Runnable() {

@Override

public void run() {

try{

//1 得到链接对象

String path = "192.168.0.184:8080/";

URL url = new URL(path);

HttpURLConnection connection = (HttpURLConnection) nnection();

nectTimeout(5000);

dTimeout(10000);

t();

int responseCode = ponseCode();

if(responseCode == 200){

//设置dialog最大进度

(tentLength());

InputStream is = utStream();

FileOutputStream fos = new FileOutputStream(apkFile);

byte[] buffer = new byte[1024];

int len = -1;

while ((len = (buffer)) != -1){

(buffer, 0, len);

entProgressBy(len);

}

();

();

}

nect();

//主线程,移除dialog,启动安装

runOnUiThread(new Runnable() {

@Override

public void run() {

s();

installAPK();

}

}); }catch (Exception e){

tackTrace();

}

}

}).start();

}

/**

* 启动安装apk

*/

private void installAPK() {

Intent intent = new Intent("L_PACKAGE");

aAndType(le(apkFile), "application/e-archive");

startActivity(intent);

}

}

使用异步任务下载

package r;

import ssDialog;

import ;

import ;

import ask;

import patActivity;

import ;

import ;

import ;

import tputStream;

import tream;

import LConnection;

import ;

public class AsyncTaskActivity extends AppCompatActivity {

private File apkFile;

private ProgressDialog dialog;

@Override

protected void onCreate(Bundle savedInstanceState) {

te(savedInstanceState);

setContentView(ty_async_task);

}

public void downloadAPK(View v){

//启动异步任务处理

new AsyncTask() {

//1主线程显示提示试图

@Override

protected void onPreExecute() {

dialog = new ProgressDialog();

gressStyle(_HORIZONTAL);

();

apkFile = new File(getExternalFilesDir(null), "");

}

//2分线程,联网请求

@Override

protected Void params) {

try{

//1 得到链接对象

String path = "192.168.0.184:8080/";

URL url = new URL(path);

HttpURLConnection connection = (HttpURLConnection) nnection();

nectTimeout(5000);

dTimeout(10000);

t();

int responseCode = ponseCode();

if(responseCode == 200){

//设置dialog最大进度

(tentLength());

InputStream is = utStream();

FileOutputStream fos = new FileOutputStream(apkFile);

byte[] buffer = new byte[1024];

int len = -1;

while ((len = (buffer)) != -1){

(buffer, 0, len);

//在分线程发布进度

publishProgress(len);// entProgressBy(len);

}

();

();

}

nect();

}catch (Exception e){

tackTrace();

}

return null;

}

//3主线程,更新界面

@Override

protected void onPostExecute(Void aVoid) {

s();

installAPK();

}

//在主线程中更新进度 (在publishProgress之后执行)

@Override

protected void values) {

entProgressBy(values[0]);

}

}.execute();

}

/**

* 启动安装apk

*/

private void installAPK() {

Intent intent = new Intent("L_PACKAGE");

aAndType(le(apkFile), "application/e-archive");

startActivity(intent);

}

}

  

本文标签: 下载启动对象显示线程