apk的下载和安装

来源:互联网 发布:tp框架隐藏index.php 编辑:程序博客网 时间:2024/05/22 18:21

进入页面直接弹出版本对话框

MainActivity.this.runOnUiThread(new Runnable() {    @Override    public void run() {        dialog();    }});
//实现对画框

private void dialog() {    if (status == 1) {        new AlertDialog.Builder(this)                .setIcon(R.mipmap.ic_launcher_round)                .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) {                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);}

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 欧美姿化妆品专柜 欧美姿化妆品 欧美姿化妆品怎么样 欧美姿男士洗面奶 欧美姿白玲珑系列 欧美姿化妆品旗舰店 欧美姿代理商 欧美姿红颜 欧美姿男士 欧美姿面膜 欧美姿面膜多少钱一盒 欧美姿面膜代理 欧美姿 欧美姿化妆品被曝光 欧美姿厂家 欧美姿面膜总代理 欧美姿化妆品的价格 欧美姿批发 欧美姿面膜价格 欧美姿 面膜 欧美姿化妆品好不好 欧美姿防晒 代理欧美姿 欧美姿代理 欧美姿女装 欧美姿多少钱 欧美姿服装 欧美姿护肤 欧美姿护肤品怎么样 欧美姿怎么样 欧美姿祛痘 欧美姿产品 欧美姿旗舰店 欧美姿补水面膜 欧美姿好不好 欧美姿去角质 欧美姿精华 欧美姿丰胸 欧美姿价格表 欧美姿粉底液 欧美姿水