public class VersionUpdateService extends Service {
private LocalBinder binder = new LocalBinder();
private DownLoadListener downLoadListener;//下载任务监听回调接口
private boolean downLoading;
private int progress;
private NotificationManager mNotificationManager;
private NotificationUpdaterThread notificationUpdaterThread;
private Notification.Builder notificationBuilder;
private final int NOTIFICATION_ID = 100;
private VersionUpdateModel versionUpdateModel;
private CheckVersionCallBack checkVersionCallBack;//检查结果监听回调接口
public interface DownLoadListener {
void begain();
void inProgress(float progress, long total);
void downLoadLatestSuccess(File file);
void downLoadLatestFailed();
}
public interface CheckVersionCallBack {
void onSuccess();
void onError();
}
...
private class NotificationUpdaterThread extends Thread {
@Override
public void run() {
while (true) {
notificationBuilder.setContentTitle("正在下载更新" + progress + "%"); // the label of the entry
notificationBuilder.setProgress(100, progress, false);
...
}
}
}
private void starDownLoadForground() {
//创建通知栏
notificationBuilder = new Notification.Builder(this);
...
Notification notification = notificationBuilder.getNotification();
startForeground(NOTIFICATION_ID, notification);
}
private void stopDownLoadForground() {
stopForeground(true);
}
//执行版本检查任务
public void doCheckUpdateTask() {
//获取本定版本号
final int currentBuild = AppUtil.getVersionCode(this);
//调用版本检查接口
ApiManager.getInstance().versionApi.upgradeRecords(currentBuild, new RequestCallBack() {
@Override
public void onSuccess(Headers headers, String response) {
versionUpdateModel = JSON.parseObject(response, VersionUpdateModel.class);
...
if (checkVersionCallBack != null)
checkVersionCallBack.onSuccess();
}
@Override
public void onError(int code, String response) {
...
}
});
}
public void doDownLoadTask() {
starDownLoadForground();
//启动通知栏进度更新线程
notificationUpdaterThread = new NotificationUpdaterThread();
notificationUpdaterThread.start();
//文件下载存放路径
final File fileDir = FolderUtil.getDownloadCacheFolder();
...
downLoading = true;
if (downLoadListener != null) {
downLoadListener.begain();
}
NetManager.getInstance().download(url, fileDir.getAbsolutePath(), new DownloadCallBack() {
@Override
public void inProgress(float progress_, long total) {
...
//执行进度更新
if (downLoadListener != null)
downLoadListener.inProgress(progress_, total);
}
@Override
public void onSuccess(Headers headers, String response) {
//执行成功回调
...
installApk(destFile, VersionUpdateService.this);
}
@Override
public void onError(int code, String response) {
...
//执行失败回调
}
});
}
//安装apk
public void installApk(File file, Context context) {
...
}
}
public class VersionUpdateHelper implements ServiceConnection {
private Context context;
private VersionUpdateService service;
private AlertDialog waitForUpdateDialog;
private ProgressDialog progressDialog;
private static boolean isCanceled;
private boolean showDialogOnStart;
public static final int NEED_UPDATE = 2;
public static final int DONOT_NEED_UPDATE = 1;
public static final int CHECK_FAILD = -1;
public static final int USER_CANCELED = 0;
private CheckCallBack checkCallBack;
public interface CheckCallBack{
void callBack(int code);
}
public VersionUpdateHelper(Context context) {
this.context = context;
}
public void startUpdateVersion() {
if (isCanceled)
return;
if (isWaitForUpdate() || isWaitForDownload()) {
return;
}
if (service == null && context != null) {
context.bindService(new Intent(context, VersionUpdateService.class), this, Context.BIND_AUTO_CREATE);
}
}
public void stopUpdateVersion() {
unBindService();
}
private void cancel() {
isCanceled = true;
unBindService();
}
private void unBindService() {
if (isWaitForUpdate() || isWaitForDownload()) {
return;
}
if (service != null && !service.isDownLoading()) {
context.unbindService(this);
service = null;
}
}
...
private void showNotWifiDownloadDialog() {
final AlertDialog.Builder builer = new AlertDialog.Builder(context);
builer.setTitle("下载新版本");
builer.setMessage("检查到您的网络处于非wifi状态,下载新版本将消耗一定的流量,是否继续下载?");
builer.setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
...
//如果是强制更新 exit app
if (mustUpdate) {
MainApplication.getInstance().exitApp();
}
}
});
builer.setPositiveButton("继续下载", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
service.doDownLoadTask();
}
});
...
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
service = ((VersionUpdateService.LocalBinder) binder).getService();
service.setCheckVersionCallBack(new VersionUpdateService.CheckVersionCallBack() {
@Override
public void onSuccess() {
VersionUpdateModel versionUpdateModel = service.getVersionUpdateModel();
//EventBus控制更新红点提示
EventBus.getDefault().postSticky(versionUpdateEvent);
if (!versionUpdateModel.isNeedUpgrade()) {
if(checkCallBack != null){
checkCallBack.callBack(DONOT_NEED_UPDATE);
}
cancel();
return;
}
if (!versionUpdateModel.isMustUpgrade() && !showDialogOnStart) {
cancel();
return;
}
if(checkCallBack != null){
checkCallBack.callBack(NEED_UPDATE);
}
final AlertDialog.Builder builer = ...//更新提示对话框
builer.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
if (NetUtil.isWifi(context)) {
service.doDownLoadTask();
} else {
showNotWifiDownloadDialog();
}
}
});
//当点取消按钮时进行登录
if (!versionUpdateModel.isMustUpgrade()) {
builer.setNegativeButton("稍后更新", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
cancel();
if(checkCallBack != null){
checkCallBack.callBack(USER_CANCELED);
}
}
});
}
builer.setCancelable(false);
waitForUpdateDialog = builer.create();
waitForUpdateDialog.show();
}
@Override
public void onError() {
unBindService();
...
}
});
service.setDownLoadListener(new VersionUpdateService.DownLoadListener() {
@Override
public void begain() {
VersionUpdateModel versionUpdateModel = service.getVersionUpdateModel();
if (versionUpdateModel.isMustUpgrade()) {
progressDialog = ...//生成进度条对话框
}
}
@Override
public void inProgress(float progress, long total) {
...//更新进度条
}
@Override
public void downLoadLatestSuccess(File file) {
...//执行成功处理
unBindService();
}
@Override
public void downLoadLatestFailed() {
...//执行失败处理
unBindService();
}
});
service.doCheckUpdateTask();
}
...
}
private VersionUpdateHelper versionUpdateHelper;
@Override
protected void onResume() {
super.onResume();
if(versionUpdateHelper == null)
versionUpdateHelper = new VersionUpdateHelper(this);
versionUpdateHelper.startUpdateVersion();
}
@Override
protected void onPause() {
super.onPause();
if(versionUpdateHelper != null)
versionUpdateHelper.stopUpdateVersion();
}
SettingActivity.java
private VersionUpdateHelper versionUpdateHelper;
@OnClick(R.id.rl_version_update)
public void onClickVersionUpdate(View view) {
if(updateTips.getVisibility() == View.VISIBLE){
return;
}
VersionUpdateHelper.resetCancelFlag();//重置cancel标记
if (versionUpdateHelper == null) {
versionUpdateHelper = new VersionUpdateHelper(this);
versionUpdateHelper.setShowDialogOnStart(true);
versionUpdateHelper.setCheckCallBack(new VersionUpdateHelper.CheckCallBack() {
@Override
public void callBack(int code) {
//EventBus发送消息通知红点消失
VersionUpdateEvent versionUpdateEvent = new VersionUpdateEvent();
versionUpdateEvent.setShowTips(false);
EventBus.getDefault().postSticky(versionUpdateEvent);
}
});
}
versionUpdateHelper.startUpdateVersion();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有