1.检测版本
private void checkVersion() {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(NetUtils.checkveision+"/"+packageCode(this)+"/1")
.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 {
if (response.isSuccessful()) {//回调的方法执行在子线程。
final String info=response.body().string();
final CheckBean rdb= new Gson().fromJson(info, CheckBean.class);
if(1000==(rdb.getCode())){
runOnUiThread(new Runnable()
{
public void run() {
if(rdb.getData().isUpdateFlag()){
normalUpdate(MainActivity.this,rdb);
}
}
}); } } }}); }
2,弹出更新提示
/**
* 正常升级,用户可以选择是否取消升级
*
* @param context
*/
private void normalUpdate(final Context context,final CheckBean cb) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View view = View.inflate(this, R.layout.gengxindialog, null);
builder.setView(view);
builder.setCancelable(true);
final Button btn_cancel=(Button)view.findViewById(R.id.btn_cancel);//取消按钮
final Button btn_comfirm=(Button)view.findViewById(R.id.btn_comfirm);//确定按钮
//取消或确定按钮监听事件处理
final AlertDialog dialog = builder.create();
dialog.show();
btn_comfirm.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
startDownload(cb.getData().getAppUrl().toString(),btn_comfirm);
}
});
btn_cancel.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
dialog.dismiss();
Log.i(TAG, "btn_cancel");
}
});
}
3,开启下载进程
private String getDownloadPathName() {
String cacheDir = getExternalCacheDir().getAbsolutePath();
String path = cacheDir + "/" + "比特未来" + ".apk";
return path;
}
private void startDownload(String url,final Button bt) {
if(null==url){
return;
}
String path = getDownloadPathName();
File file = new File(path);
if (file != null && file.exists()) {
file.delete();
}
FileDownloader.getImpl().create(url)
.setPath(path)
.setListener(new FileDownloadListener() {
@Override
protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
Log.i(TAG, "pending");
}
@Override
protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
// soFarBytes * 100 / totalBytes + "%"
bt.setText(soFarBytes * 100 / totalBytes + "%");
}
@Override
protected void completed(BaseDownloadTask task) {
//下载完成
Log.i(TAG, "completed");
bt.setText("下载完成");
Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
installProcess();
}
@Override
protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
Log.i(TAG, "paused");
}
@Override
protected void error(BaseDownloadTask task, Throwable e) {
Log.i(TAG, "error");
}
@Override
protected void warn(BaseDownloadTask task) {
Log.i(TAG, "warn");
}
}).start();
}
4,8.0安装权限处理
//安装应用的流程
private void installProcess() {
boolean haveInstallPermission;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//先获取是否有安装未知来源应用的权限
haveInstallPermission = getPackageManager().canRequestPackageInstalls();
if (!haveInstallPermission) {//没有权限
AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.logo);
normalDialog.setTitle("安装应用");
normalDialog.setMessage("安装应用需要打开未知来源权限,请去设置中开启权限");
normalDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startInstallPermissionSettingActivity();
}
});
normalDialog.show();
return;
}
}
//有权限,开始安装应用程序
installApk();
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void startInstallPermissionSettingActivity() {
Uri packageURI = Uri.parse("package:" + getPackageName());
//注意这个是8.0新API
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
startActivityForResult(intent, 10086);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
Log.e(TAG, "onActivityResult: ");
if (resultCode == RESULT_OK ) {
installApk();
}
super.onActivityResult(requestCode, resultCode, data);
}
5,安装应用
private void installApk() {
File apkFile = new File(getDownloadPathName());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 24) { //判读版本是否在7.0以上
//参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致 参数3 共享的文件
Uri apkUri = FileProvider.getUriForFile(MainActivity.this, "com.example.btft" + ".fileprovider", apkFile);
//添加这一句表示对目标应用临时授权该Uri所代表的文件
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
}
startActivity(intent);
}
7.0文件权限配置
AndroidManifest
file_paths.xml