容易出现的问题及思路
问题:
1 我们的签名文件必须保持一致,要不然会出现应用未安装问题。
思路:
1 首先获取我们当前应用的版本
2 把我们当前应用的版本和版本描述的信息传递给服务器
3 服务器进行版本的判断,返回结果
4 通过服务器放回的数据,我们进行判断是否弹出跟新对话框
5 在弹出对话框中通过服务器返回的下载apk的地址url,去进行下载
6 调用Android 系统安装界面进行安装
核心代码
/**
* 获取当前的apk的版本号
*/
private int getVerstionCode() {
PackageManager packageManager = getPackageManager();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
return packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return 0;
}
/**
* 弹出跟新提示框
*/
private void showUpDataDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("版本跟新提醒");
builder.setCancelable(false);
builder.setMessage(checkVersionCodeInfo.getMsg());//跟新内容
builder.setPositiveButton("立即升级", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//去下载新版
downloadApk();
}
});
builder.setNegativeButton("稍后在说", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//判断是否进入主界面还是登陆界面
yesOrNoEnterFunction();
}
});
builder.show();
}
/**
* 下载Apk
*/
private void downloadApk() {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "sd卡异常", Toast.LENGTH_SHORT).show();
return;
}
// 进度提示框
final ProgressDialog pd = new ProgressDialog(this);
// 设置为横向
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();// 显示进度框
// String url = "http://";
OkHttpUtils.post().url(downloadAddress).addHeader("key",
userKey).build().execute(new Callback<String>() {
@Override
public String parseNetworkResponse(Response response, int id) throws
Exception {
ResponseBody body = response.body();
long contentLength = body.contentLength();
pd.setMax((int) contentLength);
// 获取下载的文件流
InputStream inputStream = body.byteStream();
// 存放路径
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/sunmi.apk";
FileOutputStream fos = null;
try {
File file = new File(path);// 目标的File对象
fos = new FileOutputStream(file);
int len = 0;
byte[] buffer = new byte[1024];
int progress = 0;// 进度
// 写入文件
while ((len = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, len);
progress += len;// 进度增加
pd.setProgress(progress);// 设置进度
}
SystemClock.sleep(30);
installApk(file);
} catch (Exception e) {
e.printStackTrace();
// 进主页面
// enterHome();
yesOrNoEnterFunction();
} finally {
// 关闭流
StreamUtils.close(fos);
// 弹出框消失
pd.dismiss();
}
return null;
}
@Override
public void onError(Call call, Exception e, int id) {
yesOrNoEnterFunction();
}
@Override
public void onResponse(String response, int id) {
}
});
}
/**
* 安装apk
*/
private void installApk(File file) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
// 如果要安装完后 显示打开页面 就添加下面这句代码
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 参1 uri
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
startActivityForResult(intent, REQUEST_CODE_INSTALL);
}
网友评论