1.当我们把本地的版本号和服务器上的版本号进行对比,如果不一致,那么就进行版本更新,为了增加App的友好性,我们最好加一层Dialog框进行询问是否更新,当点击确定时再弹出popupwindow进行下载新apk
2.弹出popupwindow,开始下载
FileDownloader的git地址:https://github.com/lingochamp/FileDownloader
记得导依赖和初始化
public void showpopuwindow(){
//获取屏幕的宽高
DisplayMetrics dm = getResources().getDisplayMetrics();
int heigth = dm.heightPixels;
int width = dm.widthPixels;
//这是popupwindow的布局文件
View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.my_layout, null);
//进度条
final ProgressBar bar = inflate.findViewById(R.id.bar);
final PopupWindow popupWindow=new PopupWindow(inflate,width,heigth);
//设置点击外部popupwindow不会消失
popupWindow.setOutsideTouchable(false);
popupWindow.setFocusable(false);
popupWindow.showAtLocation(layout, Gravity.CENTER,0,0);
//弹出时背景变暗
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.6f;
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getWindow().setAttributes(lp);
//这里是popupwindow里面的取消
Button pass = inflate.findViewById(R.id.pass);
//这里是popupwindow里面的进度textview
sum = inflate.findViewById(R.id.sum);
pass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
//当点击取消时会暂停下载任务
FileDownloader.getImpl().pauseAll();
//当popupwindow消失时把背景变亮,就是变暗的那里0.6f变成1.0f即可
}
});
里面的文件下载位置举例
public static final String videoName ="懂车帝.apk";
public static final String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/miniVideo";
private String videoUrl="http://yapkwww.cdn.anzhi.com/data4/apk/201809/05/88b15f12830c0dd4da944d890375e548_64538300.apk";
//开始下载apk文件 videoUrl文件下载地址
FileDownloader.getImpl().create(videoUrl).setWifiRequired(false).setPath(path + "/" + videoName).setListener(new FileDownloadListener() {
@Override
protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
System.out.println("pending"+totalBytes);
//down.setMaxValue(totalBytes);
}
@Override
protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
System.out.println("connected");
bar.setMax(totalBytes);
//num自己创建一个
num=totalBytes;
}
@Override
protected void blockComplete(BaseDownloadTask task) throws Throwable {
super.blockComplete(task);
System.out.println("blockComplete");
}
//进度
@Override
protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
System.out.println("progress");
bar.setProgress(soFarBytes);
int i = (int) ((float)soFarBytes / totalBytes*100);
sum.setText(i+"%");
}
//下载完成
@Override
protected void completed(BaseDownloadTask task) {
System.out.println("completed");
bar.setMax(1);
bar.setProgress(1);
sum.setText(100+"%");
popupWindow.dismiss();
//进行安装app
File file=new File(path + "/" + videoName);
installApk(file);
}
@Override
protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
System.out.println("paused");
}
@Override
protected void error(BaseDownloadTask task, Throwable e) {
System.out.println("error"+e.getMessage());
}
@Override
protected void warn(BaseDownloadTask task) {
System.out.println("warn");
}
}).start();
}
//安装应用
private void installApk(File file) {
//新下载apk文件存储地址
File apkFile = file;
Intent intent = new Intent(Intent.ACTION_VIEW);
//判断是否是AndroidN以及更高的版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".fileProvider", apkFile);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
startActivity(intent);
}
//要想安装应用还需要两步,不然安装失败
第一步:
首先在清单文件中进行配置
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="你的包名.fileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" /> //第二步中写
</provider>
第二步:在res下面新建一个xml文件夹,并在其下创建一个xml文件 file_paths
<paths>
<external-path path="Android/data/你的包名/" name="files_root" />
<external-path path="." name="external_storage_root" /> //除了包名,其他都可改可不改,.的意思是什么路径都可以
</paths>
这样就可以安装了
还有就是记得加权限网络和读写