android app开发之app本地更新

来源:互联网 发布:win10设置网络优先级 编辑:程序博客网 时间:2024/05/02 02:42

android app通过本地接口的实现更新功能:

在这里通过xutils3下载文件,框架在这里我就不做详细的概述。如需了解请点击后面的链接查看官方文档。点击打开链接

如果需要直接使用请在在build.gradle 文件中添加 

compile 'org.xutils:xutils:3.3.40'


public class UpdateActivity extends AppCompatActivity {    ProgressDialog progressDialog;    private String BASE_PATH;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_update);        if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {            /* 得到SD卡得路径 */            BASE_PATH = Environment.getExternalStorageDirectory().getPath() + "/zhuawar.apk";            /* 更新所有录音文件到List中 */            doNewVersionUpdate();        } else {            Toast.makeText(this, "没有SD卡", Toast.LENGTH_LONG).show();        }    }    private void doNewVersionUpdate() {//        int verCode = Config.getVerCode(this);//        String verName = Config.getVerName(this);        StringBuffer sb = new StringBuffer();        sb.append("当前版本:");        sb.append("1.2.2");        sb.append(" Code:");        sb.append("0");        sb.append(", 发现新版本:");        sb.append("1.2.3");        sb.append(" Code:");        sb.append("0");        sb.append(", 是否更新?");        Dialog dialog = new AlertDialog.Builder(this)                .setTitle("软件更新")                .setMessage(sb.toString())// 设置内容                .setPositiveButton("更新",// 设置确定按钮                        new DialogInterface.OnClickListener() {                            @Override                            public void onClick(DialogInterface dialog,                                                int which) {                                progressDialog = new ProgressDialog(UpdateActivity.this);                                progressDialog.setProgressNumberFormat("%1d kb/%2d kb");                                progressDialog.setTitle("正在下载");                                progressDialog.setMessage("请稍候...");                                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);                                progressDialog.show();                                downloadFile("http://www.aidpet.cn/zhuawar.apk", BASE_PATH);                            }                        })//                .setNegativeButton("暂不更新",//                        new DialogInterface.OnClickListener() {//                            public void onClick(DialogInterface dialog,//                                                int whichButton) {//// 点击"取消"按钮之后退出程序//                                finish();//                            }//                        })                .create();// 创建// 显示对话框        dialog.show();    }    /**     * 下载app     *     * @param url  app的下载链接     * @param path 保存地址     */    private void downloadFile(final String url, String path) {        RequestParams requestParams = new RequestParams(url);        requestParams.addHeader("Accept-Encoding", "identity");        requestParams.setSaveFilePath(path);        x.http().get(requestParams, new Callback.ProgressCallback<File>() {            @Override            public void onWaiting() {            }            @Override            public void onStarted() {            }            @Override            public void onLoading(long total, long current, boolean isDownloading) {                progressDialog.setMax((int) total);                progressDialog.setProgress((int) current);            }            @Override            public void onSuccess(File result) {                Toast.makeText(UpdateActivity.this, "下载成功", Toast.LENGTH_SHORT).show();                progressDialog.dismiss();                installPackage();            }            @Override            public void onError(Throwable ex, boolean isOnCallback) {                ex.printStackTrace();                Toast.makeText(UpdateActivity.this, "下载失败,请检查网络和SD卡", Toast.LENGTH_SHORT).show();                progressDialog.dismiss();            }            @Override            public void onCancelled(CancelledException cex) {            }            @Override            public void onFinished() {            }        });    }    /**     * 安装下载好的apk文件     */    private void installPackage() {        File file = new File(BASE_PATH);        if (file.exists()) {            Intent intent = new Intent();            intent.setAction("android.intent.action.VIEW");            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");            startActivity(intent);        } else {            ToastUtil.showShortToast(this, "文件下载错误,请重新下载!");        }    }}

0 0
原创粉丝点击