public class MainActivity extends Activity {
private Button button;
// 更新版本接口
private String apkUrl = "http://m.yunifang.com/yunifang/mobile/version?random=67603&encode=e441af3089cbeeb6b58ac72393a66667&clientType=android&clientVersion=10000";
//下载保存的地址
private String sdPath = "/sdcard/" + System.currentTimeMillis() + "我的更新.apk";
private String version;
private String version_url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
// 获得本应用的版本号
initdata();
/// 解析版本更新接口
versonData();
}
// 解析版本更新接口
public void versonData() {
HttpUtils httpUtils = new HttpUtils();
httpUtils.send(HttpMethod.GET, apkUrl, new RequestCallBack<String>() {
private VersonBean bean;
@Override
public void onFailure(HttpException arg0, String arg1) {
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
String string = arg0.result;
Gson gson = new Gson();
bean = gson.fromJson(string, VersonBean.class);
ifVersion(bean);
}
});
};
// 判断是否更新
private void ifVersion(VersonBean bean) {
String version_name = bean.data.version_Name;
version_url = bean.data.url;
if (!version_name.equals(version)) {
Toast.makeText(MainActivity.this, "要更新了", 0).show();
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("软件版本更新");
dialog.setMessage(bean.data.description);
dialog.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 点击立即下载更新包
downLoadApk();
dialog.dismiss();
}
});
dialog.setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = dialog.create();
alertDialog.show();
} else {
Toast.makeText(MainActivity.this, "不更新", 0).show();
}
}
private void initdata() {
// 获取本应用的版本号
PackageManager manager = this.getPackageManager();
PackageInfo info;
try {
info = manager.getPackageInfo(this.getPackageName(), 0);
version = info.versionName;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 下载新版apk
private void downLoadApk() {
pd = new ProgressDialog(MainActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在下载更新");
HttpUtils httpUtils = new HttpUtils();
httpUtils.download(version_url, sdPath, new RequestCallBack<File>() {
@Override
public void onSuccess(ResponseInfo<File> arg0) {
//获得apk安装地址
String path = arg0.result.getPath();
File file = new File(path);
//用隐式跳转进行安装
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
// TODO Auto-generated method stub
super.onLoading(total, current, isUploading);
pd.setMax(( int)total);
pd.setProgress(( int)current);
}
@Override
public void onStart() {
pd.show();
super.onStart();
}
@Override
public void onFailure(HttpException arg0, String arg1) {
}
});
}
}
android关于版本更新
最新推荐文章于 2023-02-08 11:05:52 发布