该更新也可用于Android6.0以上需要获取权限时,无法打开安装包的情况
首先,你需要一个让后台给你传一个类似<http:// www . xxx . com/123.apk>这样的接口,也就是下载文件的接口。
其次附上检测更新时的弹窗布局文件,名称是dialog_app_download
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/common_dialog_bg"
>
<TextView
android:layout_width="match_parent"
android:layout_height="50dip"
android:gravity="center"
android:text="提示"
android:textColor="#000000"
android:textSize="16sp"
/>
<TextView
android:id="@+id/tv_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="15dp"
android:visibility="gone"
android:textSize="12sp"
android:textColor="#4b4b4b"
/>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:max="100"
android:progressDrawable="@drawable/progress_bar_bg"
android:visibility="gone"
style="@android:style/Widget.ProgressBar.Horizontal"
/>
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="@dimen/px_28"
android:textColor="@color/tv_most_black"
android:padding="17dp"
android:text="@string/default_string"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dip"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_cancel"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="#4b4b4b"
android:textSize="16sp"
android:text="取消"
/>
<TextView
android:id="@+id/tv_sure"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textColor="@color/app_color_theme"
android:textSize="16sp"
android:text="确定"
/>
</LinearLayout>
</LinearLayout>
ps:字体颜色为你的APP的主题颜色,你可以具体情况具体运用
common_dialog_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<corners
android:radius="4dp"
></corners>
<solid
android:color="@color/white"
></solid>
</shape>
progress_bar_bg
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置背景色(蓝色) -->
<item android:id="@android:id/background" >
<shape>
<corners android:radius="1dp" />
<gradient android:startColor="#f3f3f3"
android:endColor="#f3f3f3" />
</shape>
</item>
<!-- 设置进度条颜色(红色) -->
<item android:id="@android:id/progress" >
<clip>
<shape>
<corners android:radius="1dp" />
<gradient android:startColor="@color/app_color_theme"
android:endColor="@color/app_color_theme" />
</shape>
</clip>
</item>
</layer-list>
ps:字体颜色为你的APP的主题颜色,你可以具体情况具体运用
在构造函数中初始化
private Context mContext;
private ProgressBar mProgressBar;
private TextView tv_cancel,tv_sure,tv_content,tv_progress;
private String down_url;
private File mFile;
private String channelName;
private String banben;
public AppDownloadDialog(@NonNull Context context) {
super(context, R.style.mask_dialog);
mContext = context;
View view = LayoutInflater.from(context).inflate(R.layout.dialog_app_download,null,false);
setContentView(view);
mProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
tv_sure = (TextView) view.findViewById(R.id.tv_sure);
tv_content = (TextView) view.findViewById(R.id.tv_content);
tv_progress = (TextView) view.findViewById(R.id.tv_progress);
tv_sure.setOnClickListener(this);
tv_cancel.setOnClickListener(this);
WindowManager.LayoutParams lp = getWindow()
.getAttributes();
lp.width = (ConfigYibaisong.window_x * 3) / 4;
lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
getWindow().setAttributes(lp);
try {
channelName = AnalyticsConfig.getChannel(context);
} catch (Exception e) {
e.printStackTrace();
channelName="";
}
}
核心逻辑
if(mFile!=null){
installApk(mFile);
}else {
//此处为核心代码,以OkGo为基础实现
showProgress();
OkGo.<File>get(down_url).execute(new FileCallback(Environment.getExternalStorageDirectory().getPath(),"YiLinCollege"+banben+".apk") {
@Override
public void onSuccess(Response<File> response) {
mFile = response.body();
installApk(mFile);
tv_cancel.setEnabled(true);
tv_sure.setEnabled(true);
}
@Override
public void downloadProgress(Progress progress) {
super.downloadProgress(progress);
mProgressBar.setProgress((int) (progress.fraction*100));
tv_progress.setText(((int) (progress.fraction*100))+"%");
}
@Override
public void onError(Response<File> response) {
super.onError(response);
dismiss();
AppToastMgr.showToast(response.getException().toString());
}
});
}
以下为上述代码中的showProgress()
方法
private void showProgress(){
tv_content.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
tv_progress.setVisibility(View.VISIBLE);
tv_cancel.setEnabled(false);
tv_sure.setEnabled(false);
setCanceledOnTouchOutside(false);
}
以下为跳转至安装apk界面代码
private void installApk(File file){
if(file==null)
return;
String fileName = file.getName();
if (fileName.endsWith(".apk")) {
Intent install = new Intent(Intent.ACTION_VIEW);
if(Build.VERSION.SDK_INT>=24) {//判读版本是否在7.0以上
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件
} else{
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
install.setDataAndType(FileProviderUtils.getUriForFile(mContext,file),
"application/vnd.android.package-archive");
mContext.startActivity(install);
}
}
最后附上设置数据及取消弹框的代码
public void setValue(String download, String content,String banben) {
this.down_url = download;
if(tv_content!=null)
tv_content.setText(content);
this.banben = banben;
}
public void hiddenCancel(){
if(tv_cancel!=null){
tv_cancel.setVisibility(View.GONE);
}
}
在activity中的应用
PackageManager pm = MainActivity.this.getPackageManager();
PackageInfo pi = pm.getPackageInfo(MainActivity.this.getPackageName(), 0);
if(yourcode>pi.versionCode){
//在这可以加个判断版本号的逻辑
}
AppDownloadDialog dialog_down = new AppDownloadDialog(MainActivity.this);
dialog_down.setValue("your down_url","your content",pi.versionName+"");
if(must==1){
dialog_down.hiddenCancel();
dialog_down.setCanceledOnTouchOutside(false);
}else{
dialog_down.setCanceledOnTouchOutside(true);
}
dialog_down.show();
完美收工
ps:主要用于检测新版本之后,运用此弹窗工具类
网友评论