版本更新
来源:互联网 发布:淘宝综合评分500 编辑:程序博客网 时间:2024/05/10 13:31
Manifest添加权限
<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>x.utils、gson jar包
@ContentView(R.layout.activity_main)public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private String targetPath = ""; private ProgressDialog progressDialog; private Nows.ResultBean result1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); x.view().inject(this); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File file = Environment.getExternalStorageDirectory(); targetPath = file.getAbsolutePath() + File.separator; } progressDialog = new ProgressDialog(this); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); } @Event(R.id.btn_version) private void btnChick(View v) { chenckVerson(); } private void chenckVerson() { int versionCode = VersionUtil.getVersionCode(this); RequestParams requestParams = new RequestParams("http://172.18.47.69/checkversion.php"); requestParams.addQueryStringParameter("version", String.valueOf(versionCode)); x.http().get(requestParams, new Callback.CommonCallback<String>() { @Override public void onSuccess(final String result) { Gson gson = new Gson(); Nows nows = gson.fromJson(result, Nows.class); //是否有更新 if (nows.isSuccess()) { //有更新 result1 = nows.getResult(); //如果是强制更新 if (result1.isHas_new_version()) { if (result1.isMust_update()) { new AlertDialog.Builder(MainActivity.this).setTitle("版本更新").setMessage("升级到最新版本").setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.e(TAG, "onClick: "+result1.getUrl() ); updateVersion(result1.getUrl()); dialog.dismiss(); } }).setCancelable(false).create().show(); } else { new AlertDialog.Builder(MainActivity.this).setTitle("版本更新").setMessage("是否要更新最新版本").setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,result1.getUrl(),Toast.LENGTH_SHORT).show(); updateVersion(result1.getUrl()); dialog.dismiss(); } }).setNegativeButton("下次再说", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).create().show(); } } else { Toast.makeText(MainActivity.this, "已经是最新版本", Toast.LENGTH_SHORT).show(); } } } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } private void updateVersion(String url) { targetPath = targetPath + System.currentTimeMillis() + ".apk"; RequestParams params = new RequestParams(url); // 设置下载保存路径 params.setSaveFilePath(targetPath); // xutils的文件下载 x.http().get(params, new Callback.ProgressCallback<File>() { @Override public void onSuccess(File result) { if (progressDialog.isShowing()) { progressDialog.dismiss(); } installApk(result); } @Override public void onError(Throwable ex, boolean isOnCallback) { if (progressDialog.isShowing()) { progressDialog.dismiss(); } Log.e(TAG, "onError: "+ex.getMessage() ); Toast.makeText(MainActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } @Override public void onWaiting() { } @Override public void onStarted() { } @Override public void onLoading(long total, long current, boolean isDownloading) { if (isDownloading) { progressDialog.setMessage("正在下载..."); progressDialog.show(); progressDialog.setMax((int) total); progressDialog.setProgress((int) current); } } }); } /** * 安装apk * * @param file */ private void installApk(File file) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.addCategory("android.intent.category.DEFAULT"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startActivity(intent); android.os.Process.killProcess(android.os.Process.myPid()); }}
public class Nows { private boolean success; private String message; private ResultBean result; public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public ResultBean getResult() { return result; } public void setResult(ResultBean result) { this.result = result; } public static class ResultBean { /** * has_new_version : true * must_update : true * url : http://169.254.149.71/download/jinritoutiao.apk */ private boolean has_new_version; private boolean must_update; private String url; public boolean isHas_new_version() { return has_new_version; } public void setHas_new_version(boolean has_new_version) { this.has_new_version = has_new_version; } public boolean isMust_update() { return must_update; } public void setMust_update(boolean must_update) { this.must_update = must_update; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }}
VersionUtil类
public class VersionUtil { private static int versionCode=0; public static int getVersionCode(Context context){ try { PackageManager packageManager=context.getPackageManager(); String packageName = context.getPackageName(); PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0); versionCode = packageInfo.versionCode; } catch (Exception e) { e.printStackTrace(); } return versionCode; }}
BaseApplication类
public class BaseApplication extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this); x.Ext.setDebug(BuildConfig.DEBUG); }}
修改ip地址,将apk复制到download中,修改download和.php的名称,保持一致
阅读全文
0 0
- 版本更新
- //版本更新
- 版本更新
- 更新版本
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 版本更新
- 网易笔试 分饼干
- JavaWeb学习总结(十)——HttpServletRequest对象(一)
- MPP数据库
- 关于Java多线程------(4,经典案例---生产者和消费者)
- iOS
- 版本更新
- C# datagridview大小跟随窗口动态改变
- 机器学习-周志华-笔记-第2章
- 两种解决异常:“The last packet sent successfully to the server was 0 milliseconds ago. ”的办法
- Gabor滤波器学习
- c# 中出现 可访问性不一致的原因与解决方法
- 谈谈Spark GraphX吧!
- Unity中depth pass的优化以及谈谈unity中的Framebuffer
- 比特币甘道夫