public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button button_1; private Button button_2; private String latest; private String msg; private int status; private String url; private Intent intent; private NotificationManager mNotifyManager; private NotificationCompat.Builder mBuilder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initOkHttp(); initView(); } //实例化 private void initOkHttp() { OkHttpClient okHttpClient = new OkHttpClient(); final Request request = new Request.Builder() .url("https://news-at.zhihu.com/api/4/version/android/2.3.0") .build(); Call call = okHttpClient.newCall(request); //请求加入调度 call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); try { JSONObject jsonObject = new JSONObject(string); latest = jsonObject.getString("latest"); msg = jsonObject.getString("msg"); status = jsonObject.getInt("status"); url = jsonObject.getString("url"); Log.d("zdl", "" + latest + msg + status + url); MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { dialog(); } }); } catch (JSONException e) { e.printStackTrace(); } } }); } private void initView() { button_1 = (Button) findViewById(R.id.button_1); button_2 = (Button) findViewById(R.id.button_2); intent = new Intent(); button_1.setOnClickListener(this); button_2.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_1: intent.setClass(MainActivity.this, Main2Activity.class); break; case R.id.button_2: intent.setClass(MainActivity.this, Main3Activity.class); break; } startActivity(intent); } //弹出对话框 private void dialog() { if (status == 1) { new AlertDialog.Builder(this) .setIcon(R.mipmap.ic_launcher) .setTitle("发现新版本") .setMessage(msg) .setPositiveButton("升级", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { downLoadApk(); //创建通知. createNotification(MainActivity.this); Toast.makeText(MainActivity.this, "开始升级", Toast.LENGTH_SHORT).show(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { // intent.setClass(MainActivity.this, Main2Activity.class); // startActivity(intent); Toast.makeText(MainActivity.this, "你点击了取消升级", Toast.LENGTH_SHORT).show(); } }).create().show(); } else { Toast.makeText(this, "当前版本是" + latest, Toast.LENGTH_SHORT).show(); } } //下载后文件保存的目录. private String downLoadPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/downloads/"; private void downLoadApk() { //创建请求参数 RequestParams requestParams = new RequestParams(url); requestParams.setSaveFilePath(downLoadPath + "nihao"); x.http().request(HttpMethod.GET, requestParams, new org.xutils.common.Callback.ProgressCallback<File>() { @Override public void onSuccess(File file) { } @Override public void onError(Throwable throwable, boolean b) { } @Override public void onCancelled(CancelledException e) { } @Override public void onFinished() { } @Override public void onWaiting() { } @Override public void onStarted() { } //total:文件总大小,current,当前进度.isDownloading,是否正在下载. @Override public void onLoading(long total, long current, boolean isDownloading) { notifyNotification(current, total); if (total == current) {//若下载完成 mBuilder.setContentText("下载完成"); mNotifyManager.notify(10086, mBuilder.build()); //安装apk文件 installApk(MainActivity.this,new File(downLoadPath,"nihao")); } } }); } // 创建通知栏进度条 private void createNotification(Context context) { mNotifyManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); mBuilder = new NotificationCompat.Builder(context); mBuilder.setSmallIcon(R.mipmap.ic_launcher_round); mBuilder.setContentTitle("版本更新"); mBuilder.setContentText("正在下载..."); mBuilder.setProgress(0, 0, false); Notification notification = mBuilder.build(); notification.flags = Notification.FLAG_AUTO_CANCEL; //参数1:通知id,参数:通知. mNotifyManager.notify(10086, notification); } /** * 更新通知栏进度条 */ private void notifyNotification(long percent, long length) { mBuilder.setProgress((int) length, (int) percent, false); mNotifyManager.notify(10086, mBuilder.build()); } /** * 安装apk * * @param context 上下文 * @param file APK文件 */ private void installApk(Context context, File file) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); context.startActivity(intent); } }
版本更新
最新推荐文章于 2024-03-20 16:21:21 发布