中国市场才需要这些emmm 以下是android代码 没有ios的。。google市场不需要

需要的数据

1  把安装包放到服务器生成的url地址

2  升级描述

3  是否强制升级

4  当前应用版本号

步骤

1  判断下载地址 有sd卡存哪里 没有sd卡存哪里 此步骤要尽早完成

所以可以卸载MyApplication的onCreate方法中

try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
downLoadUrl = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getPath();
saveDir = new File(getExternalFilesDir(Environment.DIRECTORY_MOVIES).getPath() + "/download");
if (!saveDir.exists())
saveDir.mkdir();
} else {
downLoadUrl = getFilesDir().getPath();
saveDir = new File(getFilesDir().getPath() + "/download");
if (!saveDir.exists())
saveDir.mkdir();
}
} catch (Exception e) {
}

2 请求数据   对比版本  下载安装包 然后再安装上。就完成了

 

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.view.View.OnClickListener;



import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
* 下载更新
*
* @author Smoke
*/
@SuppressLint("HandlerLeak")
public class UpdateManager {
public static UpdateManager mInstance;
/* 下载中 */
private static final int DOWNLOAD = 1;
/* 下载结束 */
private static final int DOWNLOAD_FINISH = 2;
/* 下载保存路径 */
private String mSavePath;
/* 记录进度条数量 */
private int progress;
// 更新路径
private String URL;
// 最新版本更新日志
private String log;
// apk名称
private String name = "yingyongname.apk";

private boolean isMust = false;

private Context context;

private boolean isDownLoading = false;

private static boolean isFinish;
//非强制更新
private DialogCustomAlert_Title updateDialog;

private DialogCustomAlert_Title_One_Click updateDialog_must;

private UpdateManager(Context context) {
this.context = context;
}

public static UpdateManager getInstance(Context context) {
mInstance = new UpdateManager(context);
mInstance.mSavePath = MyApplication.downLoadUrl;
return mInstance;
}

private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
// 正在下载
case DOWNLOAD:
// 设置进度条位置
// mDownloadDialog.setProgress(progress);
if (!isMust) {
if (updateDialog != null && updateDialog.isShowing()) {
updateDialog.setRightName(progress + "%");
}
} else {
if (updateDialog_must != null && updateDialog_must.isShowing()) {
updateDialog_must.setLeftName(progress + "%");
}
}
break;
case DOWNLOAD_FINISH:
isDownLoading = false;
isFinish = true;
if (!isMust) {
if (updateDialog != null && updateDialog.isShowing()) {
updateDialog.setRightName("立即安装");
}
} else {
if (updateDialog_must != null && updateDialog_must.isShowing()) {
updateDialog_must.setLeftName("立即安装");
}
}
break;
default:
break;
}
}
};

/**
* 检测软件更新(可选更新)
*
* @param log
*/
public static void Update(Context context, String url, String log) {
getInstance(context);
mInstance.URL = url;
mInstance.isMust = false;
mInstance.showNoticeDialog(context, log);
}

/**
* 检测软件更新(必须更新)
*
* @param log
*/
public static void MustUpdate(Context context, String url, String log) {
getInstance(context);
mInstance.URL = url;
mInstance.isMust = true;
mInstance.showMustNoticeDialog(context, log);
}

/**
* 显示软件更新对话框
*/
private void showNoticeDialog(Context context, String log) {
if (updateDialog == null) {
updateDialog = DialogCustomAlert_Title.getIstance(context, "发现新版本", log,
"以后再说", "立即更新", false, new OnClickListener() {
@Override
public void onClick(View v) {
if (!isDownLoading) {
updateDialog.dismiss();
updateDialog = null;
mInstance = null;
}
}
}, new OnClickListener() {
@Override
public void onClick(View v) {
if (!isDownLoading) {
if (!isFinish) {//未下载完成
isDownLoading = true;
downloadApk();
updateDialog.setRightName("0%");
updateDialog.setLeftColor(R.color.gray);
} else {//下载完成
// 安装文件
installApk();
}
}
}
});

if (updateDialog != null)
updateDialog.setLeftColor(R.color.one_level);

if (isFinish) {
updateDialog.setRightName("立即安装");
}
}
}

/**
* 显示软件更新对话框
*/
private void showMustNoticeDialog(Context context, String log) {
if (updateDialog_must == null) {
updateDialog_must = DialogCustomAlert_Title_One_Click.getIstance(context, "发现新版本", log,
"立即更新", false, new OnClickListener() {
@Override
public void onClick(View v) {
if (!isDownLoading) {
if (!isFinish) {
isDownLoading = true;
downloadApk();
updateDialog_must.setLeftName("0%");
} else {//下载完成
// 安装文件
installApk();
}
}
}
});
}
}

/**
* 下载apk文件
*/
private void downloadApk() {
// 启动新线程下载软件
new downloadApkThread().start();
}

/**
* 下载文件线程
*/
private class downloadApkThread extends Thread {
@Override
public void run() {
try {
// 判断SD卡是否存在,并且是否具有读写权限
mSavePath = MyApplication.downLoadUrl;
java.net.URL url = new URL(URL);
// 创建连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
// 获取文件大小
int length = conn.getContentLength();
// 创建输入流
InputStream is = conn.getInputStream();

File file = new File(mSavePath);
// 判断文件目录是否存在
if (!file.exists()) {
file.mkdir();
}
File apkFile = new File(mSavePath, name);
FileOutputStream fos = new FileOutputStream(apkFile);
int count = 0;
// 缓存
byte buf[] = new byte[1024];
// 写入到文件中
do {
int numread = is.read(buf);
count += numread;
// 计算进度条位置
progress = (int) (((float) count / length) * 100);
// 更新进度
mHandler.sendEmptyMessage(DOWNLOAD);
if (numread <= 0) {
// 下载完成
mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
break;
}
// 写入文件
fos.write(buf, 0, numread);
} while (true);// 点击取消就停止下载.
fos.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

;

/**
* 安装APK文件
*/
private void installApk() {


File apkfile = new File(mSavePath, name);
if (!apkfile.exists()) {
TipsToast.showTips(context, 0, R.string.apk_bad);
isFinish = false;
isDownLoading = false;
if (!isMust) {
if (updateDialog != null && updateDialog.isShowing()) {
updateDialog.setRightName("重新下载");
}
} else {
if (updateDialog_must != null && updateDialog_must.isShowing()) {
updateDialog_must.setLeftName("重新下载");
}
}
return;
}
// 通过Intent安装APK文件
Intent i = new Intent(Intent.ACTION_VIEW);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// 声明需要的临时权限
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// 第二个参数,即第一步中配置的authorities
String packageName = ((Activity)context).getApplication().getPackageName();
Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", apkfile);
i.setDataAndType(contentUri, "application/vnd.android.package-archive");
}else {
i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
// i.setDataAndType(FileProvider.getUriForFile(apkfile), "application/vnd.android.package-archive");

i.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
}
context.startActivity(i);
}
}

多余的代码不贴了 核心科技已经展现出来了