private void downLoadPackage() {
String url = "https://qd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk";
if (VERSION.SDK_INT >= VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("download", "下载进度", IMPORTANCE_DEFAULT);
channel.setImportance(NotificationManager.IMPORTANCE_LOW);
//初始化通知ui
nb = new Notification.Builder(this, "download");
nb.setSmallIcon(R.drawable.ic_launcher_background).setContentTitle("下载中");
nb.setProgress(100, 0, false).setAutoCancel(true);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//创建通道,注意这一步很重要
manager.createNotificationChannel(channel);
//使用okhttp发送get请求
OkHttpClient client = new OkHttpClient();
Request.Builder builder = new Request.Builder();
Request request = builder.url(url).get().build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream inputStream = response.body().byteStream();
File espd = Environment.getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS);
//缓存的路径以及文件名
String apkPath = espd.getPath() + "/" + "BLiao" + System.currentTimeMillis() + ".apk";
FileOutputStream fos = new FileOutputStream(new File(apkPath));
//目标文件的总大小
long contentLength = response.body().contentLength();
int length = 0;
//字节数组缓冲区要设置为足够大,过小会导致io速度慢
byte[] bytes = new byte[10240];
while ((length = inputStream.read(bytes)) != -1) {
fos.write(bytes, 0, length);
//已经写入的文件内容所占百分数
double percent = fos.getChannel().position() * 100.0 / contentLength;
//手动刷新进度条
nb.setProgress(100, (int) percent, false);
manager.notify(1, nb.build());
}
fos.flush();
fos.close();
response.body().close();
//下载完毕关闭要隐藏通知
manager.cancel(1);
//调出系统安装应用页面
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//fileprovider要在清单文件中声明
Uri contentUri = FileProvider.getUriForFile(getApplication(), "com.caikeng.app.fileProvider", new File(apkPath));
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
startActivity(intent);
}
});
}
}
本来这个功能应该是轻松加愉快的,但是因为7.0,8.0的各种适配问题,如通知api,系统安装页api的更新,导致各种问题