Android App更新功能

来源:互联网 发布:淘宝虚拟市场怎么加入 编辑:程序博客网 时间:2024/05/17 02:36

App更新操作逻辑顺序

  1. 用户点击更新按钮或者接收到推送并进行点击进行更新

  2. 更新选择框,给客户进行选择性更新.推送界面可根据RemoteViews 设计界面,根据remoteViews.setOnClickPendingIntent(int id, PendingIntent pIntent)设置通知栏某控件点击事件

  3. 增加下载App进度条,提供可视化界面给客户更好的体验感,成功下载完成自动安装App

dialog弹出框

        1. 自定义AlertDialog.Builder dialogTips
AlertDialog.Builder dialogTips= new AlertDialog.Builder(this);        dialogTips.setTitle("提示");        dialogTips.setMessage("检测到有新的版本,是否更新?");        dialogTips.setNegativeButton("取消", null);        dialogTips.setPositiveButton("更新", new OnClickListener() {            @Override            public void onClick(DialogInterface arg0, int arg1) {                startDownload();            }        });        dialogTips.show();
    2.  startDownload()方法注释
//判断是否挂载SD,是创建APP保存地址.                   if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){            File path = new File(Environment.getExternalStorageDirectory(),                    "文件名"+ ".apk");            httpDownLoad(path.getPath(), "APk下载url");
     3. 下载App进度条ProgressDialog
        dialog=new ProgressDialog(this);        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);        dialog.setMessage("更新应用");        dialog.setMax(100);
    4. httpDownLoad(path.getPath(), "APk下载url")方法注释,第三方Xutils链接:https://github.com/wyouflf/xUtils
private void httpDownLoad(String path, String url) {        //xUtils.jar  引用HttpUtils         HttpUtils http = new HttpUtils();        http.download(url, path, true, true, new RequestCallBack<File>() {            @Override            public void onStart() {                super.onStart();                //开始显示进度条dialog                dialog.show();            }            @Override            public void onLoading(long total, long current, boolean isUploading) {                int index = (int) (current * 100 / total);                //设置dialog进度                dialog.setProgress(index);            }            @Override            public void onSuccess(ResponseInfo<File> responseInfo) {                //获取到安装包后,调用系统的android安装apk界面进行安装 这是固定格式                Intent intent = new Intent(Intent.ACTION_VIEW);                intent.setDataAndType(                        Uri.fromFile(new File(responseInfo.result.getPath())),                        "application/vnd.android.package-archive");                startActivity(intent);                dialog.dismiss();            }            @Override            public void onFailure(                    com.lidroid.xutils.exception.HttpException arg0, String arg1) {                File path = new File(Environment.getExternalStorageDirectory(),                        "文件名"  + ".apk");                Toast.makeText(MainActivity.this, "下载失败"+arg1, 0).show();                dialog.dismiss();                //下载完成清除文件,节省空间                path.delete();            }        });    }
1 0
原创粉丝点击