菜鸟初尝写博客,排版、内容不好望多多包涵……
这些功能在APP开发都是标配
以下源码都是从我完整项目搬来,可能会不全,如有不对之处请见谅。
新手小花点时间就好。老菜路过呗
如下图:
原理:
1.点击软件升级,获取安装版本号,发送版本号到服务器,根据服务器返回做判断。
2.本例返回code == 1有新版本 弹出对话框
3.点击下载
代码如下:
这是一个共类
public class UpdateManager extends Activity {
private Context mContext;
// 0为安卓,1为苹果
private final String PLATFORM = "0";
String url = "";
String urlApp = "";
private MenuDialog noticeDialog;
private Dialog downloadDialog;
/* 下载包安装路径 */
private static final String savePath = "/sdcard/zlzs/";
private static final String saveFileName = savePath
+ "UpdateDemoRelease.apk";
/* 进度条与通知ui刷新的handler和msg常量 */
private ProgressBar mProgress;
private static final int DOWN_UPDATE = 1;
private static final int DOWN_OVER = 2;
private int progress;
private Thread downLoadThread;
private boolean interceptFlag = false;
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case DOWN_UPDATE:
mProgress.setProgress(progress);
break;
case DOWN_OVER:
installApk();
break;
default:
break;
}
};
};
public UpdateManager(Context context) {
this.mContext = context;
}
// 外部接口让主Activity调用
public void checkUpdateInfo() {
showNoticeDialog();
}
/**
* 获取当前软件版本号
*/
public void showNoticeDialog() {
String versions = getVersionName();
Map<String, String> params = new HashMap<String, String>();
params.put("platform", PLATFORM);
params.put("version", versions);
Logger.dout("xxx" + params);
HttpUtilService loginService = new HttpUtilService();
loginService.AsynPost(new NetCallbackListener() {
public void onSuccess(String v) {
try {
JSONObject json = new JSONObject(v);
Logger.dout(v);
int code = json.getInt("code");
String text = json.getString("description");// 想提示文字
urlApp = json.getString("url");
if (code == 1) {
MenuDialog.Builder builder = new MenuDialog.Builder(
mContext);
builder.setTitle("软件版本更新");
builder.setMessage(text);
builder.setPositiveButton("下载", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
showDownloadDialog();
}
});
builder.setNegativeButton("以后再说",
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
noticeDialog = builder.create();
noticeDialog.show();
} else {
Toast.makeText(UpdateManager.this, "已是最新版本",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
}
}
@Override
public void onFail(String result) {
}
}, Helper.WEBSITE + "UpdateVersion", params);
}
private String getVersionName() {//获取版本号
try {
PackageManager packageManager = this.getPackageManager();
PackageInfo packInfo = packageManager.getPackageInfo(
getPackageName(), 0);
String version = packInfo.versionName;
return version;
} catch (Exception e) {
return "1.00";
}
}
private void showDownloadDialog() {
AlertDialog.Builder builder = new Builder(mContext);
builder.setTitle("软件版本更新");
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.progress, null);
mProgress = (ProgressBar) v.findViewById(R.id.progress);
builder.setView(v);
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
interceptFlag = true;
}
});
downloadDialog = builder.create();
downloadDialog.show();
downloadApk();
}
private Runnable mdownApkRunnable = new Runnable() {
@Override
public void run() {
try {
URL url = new URL(urlApp);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
File file = new File(savePath);
if (!file.exists()) {
file.mkdir();
}
String apkFile = saveFileName;
File ApkFile = new File(apkFile);
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(DOWN_UPDATE);
if (numread <= 0) {
// 下载完成通知安装
mHandler.sendEmptyMessage(DOWN_OVER);
break;
}
fos.write(buf, 0, numread);
} while (!interceptFlag);// 点击取消就停止下载.
fos.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
/**
* 下载apk
*
* @param url
*/
private void downloadApk() {
downLoadThread = new Thread(mdownApkRunnable);
downLoadThread.start();
}
/**
* 安装apk
*
* @param url
*/
private void installApk() {
File apkfile = new File(saveFileName);
if (!apkfile.exists()) {
return;
}
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + apkfile.toString()),
"application/vnd.android.package-archive");
mContext.startActivity(i);
}
}