之前项目用的友盟更新,但友盟不提供此服务了,于是我们需要自己写了
首先你需要检查更新,检查之后比对版本号:联网获取版本信息我就不写了,获取之后比对版本号,需要更新就弹出更新提示框:
if (Util.getVersionCode(context) < versionCode.getVersion()) {
if (context != null) {
//UpDataActivity是更新弹框,自己写就好
Intent intent = new Intent(context.getApplicationContext(), UpDataActivity.class);
intent.putExtra(UpDataActivity.UPDATEOBJ, versionCode);
context.startActivity(intent);
}
} else {
AppUpdateSaveMsgUtil.clear(context);
// 不需要更新时,将apk文件删除,避免占用不必要的空间
File updateDir = new File(context.getExternalFilesDir(AppConstant.UPDATEFILENAME).getAbsolutePath());
OperationFileHelper.deleteDirectory(updateDir.getPath());
if (isShowNoUpdataToast) {
Toast.makeText(context, "您现在已经是最新版本", Toast.LENGTH_LONG).show();
}
}
updateNow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
path = versionCode.getDownload_url();
// //自己写的下载逻辑
// if (!application.isUpdate()) {
// //TODO 这里你可以添加是否已经下载成功的逻辑判断如果该版本已经下载完毕,直接安装,否则启动服务下载
// Intent intent = new Intent(getApplicationContext(), UpdateService.class);
// intent.putExtra("Key_Down_Url", versionCode.getDownload_url());
// startService(intent);
// } else {
// Toast.makeText(getApplicationContext(), "APP正在下载", Toast.LENGTH_LONG).show();
// }
// 调用DownloadManager下载
Intent intent = new Intent(getApplicationContext(), ApkDownLoadService.class);
intent.putExtra(ApkDownLoadService.DownloadUrl, versionCode.getDownload_url());
intent.putExtra(ApkDownLoadService.VersionCode, versionCode.getVersion());
startService(intent);
} else {
Toast.makeText(getApplication().getApplicationContext(), "SD卡不可用,请插入SD卡",
Toast.LENGTH_SHORT).show();
}
Log.d("service", "starservice");
UpDataActivity.this.finish();
}
});
package com.example.likangxin.servicedownload.DownloadManagerDemon;
import android.app.DownloadManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import com.example.likangxin.servicedownload.AppConstant;
import com.example.likangxin.servicedownload.DownLoadFileActivity;
import java.io.File;
/**
* Created by likangxin on 16-8-26.
*/
public class ApkDownLoadService extends Service {
private String localPath;
private long mReference;
private BroadcastReceiver mReceiver;
private String down_url;
private String app_name;
private DownloadManager manager;
private long versionCode;
public static final String DownloadUrl = "DownloadUrl";
public static final String VersionCode = "VersionCode";
private Context myContext;
private Handler handler;
public static final Uri CONTENT_URI = Uri.parse("content://downloads/my_downloads");
private DownloadChangeObserver downloadObserver;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
handler = new Handler();
down_url = intent.getStringExtra(DownloadUrl);
versionCode = intent.getLongExtra(VersionCode, -1L);
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
app_name = down_url.substring(down_url.lastIndexOf("/") + 1);
myContext = this;
long downLoadId = AppUpdateSaveMsgUtil.getLong(this, AppConstant.DOWNLOADID);
if (downLoadId != -1L && versionCode == AppUpdateSaveMsgUtil.getLong(this, AppConstant.DOWNLOADVersion)) {
// Log.d("ApkDowmnloadService", "queryDownloadStatus");
queryDownloadStatus(manager);
} else {
// Log.d("ApkDowmnloadService", "down_url, app_name");
downloadRequest(down_url, app_name);
}
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Intent dialogIntent = new Intent(myContext, DownLoadFileActivity.class);//下载失败的提示框不需要可以不写
dialogIntent.putExtra(ApkDownLoadService.DownloadUrl, down_url);
dialogIntent.putExtra(ApkDownLoadService.VersionCode, versionCode);
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
}
};
return super.onStartCommand(intent, flags, startId);
}
// 执行下载
private final void downloadRequest(String downloadUrl, String mFileName) {
// Log.d("ApkDowmnloadService", "downloadRequest");
localPath = getExternalFilesDir(AppConstant.UPDATEFILENAME).getAbsolutePath();
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(downloadUrl);
DownloadManager.Request request = new DownloadManager.Request(uri);
// 设置本地缓存路径
request.setDestinationUri(Uri.fromFile(new File(localPath, mFileName)