Android开发版本更新和强制更新效果
版本更新基本每个app都有,这里加多个强制更新效果
一、思路:
用okhttp下载接口返回的apk下载地址,这样有进度条
二、效果图:
Android开发教程实战案例源码分享-版本更新和强制更新效果
三、关键代码:
// 联系:893151960
public class UpdateActivity extends AppCompatActivity {
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
mContext = this;
// 关键注意点:DownloadUtil中的installApp中的“com.cong.myupdateappdemo.FileProvider”要和清单中的android:authorities="com.cong.myupdateappdemo.FileProvider"一模一样
// 这里换成你要下载的apk链接
String apkUrl = "https://xxx.oss-cn-hangzhou.aliyuncs.com/xxx.apk";
String updateMsg = "修复已知bug,提高用户体验";
findViewById(R.id.btn_update).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateLayoutDialog(mContext, updateMsg,false, new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermission(mContext, new View.OnClickListener() {
@Override
public void onClick(View view) {
final Dialog dialog = forceUploadDialog(mContext);
ProgressBar pb = (ProgressBar)(dialog.getWindow()).findViewById(R.id.pr_download_value);
DownloadUtil.INSTANCE.versionUpdate((Activity) mContext, apkUrl, new Function1<Integer, Unit>() {
@Override
public Unit invoke(Integer integer) {
if (integer == -1 || integer == 100) dialog.dismiss();
if (integer > -1) pb.setProgress(integer);
return null;
}
});
}
}, checkSDK33(mContext) ? new String[]{
Manifest.permission.READ_MEDIA_IMAGES
} : new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
});
}
});
}
});
findViewById(R.id.btn_update_force).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateLayoutDialog(mContext, updateMsg,true, new View.OnClickListener() {
@Override
public void onClick(View v) {
requestPermission(mContext, new View.OnClickListener() {
@Override
public void onClick(View view) {
final Dialog dialog = forceUploadDialog(mContext);
ProgressBar pb = (ProgressBar)(dialog.getWindow()).findViewById(R.id.pr_download_value);
DownloadUtil.INSTANCE.versionUpdate((Activity) mContext, apkUrl, new Function1<Integer, Unit>() {
@Override
public Unit invoke(Integer integer) {
if (integer == -1 || integer == 100) dialog.dismiss();
if (integer > -1) pb.setProgress(integer);
return null;
}
});
}
}, checkSDK33(mContext) ? new String[]{
Manifest.permission.READ_MEDIA_IMAGES
} : new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
});
}
});
}
});
}
public static boolean checkSDK33(Context context) {
ApplicationInfo applicationInfo = context.getApplicationInfo();
return Build.VERSION.SDK_INT >= 33
&& applicationInfo != null
&& applicationInfo.targetSdkVersion >= 33;
}
public static void requestPermission(final Context context, final View.OnClickListener successListener, String... permissions) {
if (PermissionCheckUtil.checkPermissions(context,permissions)){
if (null != successListener) {
successListener.onClick(null);
}
} else {
PermissionAppUtils.INSTANCE.requestPermission(context, permissions, new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Throwable {
if (aBoolean){
if (null != successListener) {
successListener.onClick(null);
}
}
}
});
}
}
public static AlertDialog forceUploadDialog(final Context context) {
final AlertDialog dialog = getAlertDialog(context);
dialog.show();
Window window = dialog.getWindow();
window.setGravity(Gravity.CENTER);
window.setContentView(R.layout.dialog_force_upload_progress);
View view = window.findViewById(R.id.cl_download_app);
return dialog;
}
public void updateLayoutDialog(Context context, String msg,boolean isForce, final View.OnClickListener listener) {
final AlertDialog dialog = getAlertDialog(context);
dialog.show();
Window window = dialog.getWindow();
window.setGravity(Gravity.CENTER);
window.setContentView(R.layout.dialog_update1);
TextView tvMsg = (TextView) window.findViewById(R.id.tv_message);
if (tvMsg != null) {
tvMsg.setText(msg);
}
if (isForce){
window.findViewById(R.id.btn_cancel).setVisibility(View.GONE);
}
window.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
window.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
if (listener != null) {
listener.onClick(v);
}
}
});
}
public static AlertDialog getAlertDialog(Context context) {
return new AlertDialog.Builder(context, R.style.mydialog).setCancelable(false).create();
}
}
四、项目demo源码结构图
有问题或者需要完整源码的私信我