版本更新

来源:互联网 发布:被淘宝托管骗了怎么办 编辑:程序博客网 时间:2024/06/13 22:52

首先呢,我们是应该在用户登录后,在首页执行检查版本信息的操作,具体是,获取到本地的版本号后,提交给服务器进行判断,然后后台来告诉我们当前版本是否为最新版本,紧接着我们拿到下载地址,执行下载的操作,具体的可以使用输入输出流来对文件进行存储和读取,最后我们将下载好的文件,调用系统的安装界面,进行安装,自此我们的更新操作全部完成。

首先记得添加权限

<code class="hljs xml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.permission.INTERNET"</span>/></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.permission.WRITE_EXTERNAL_STORAGE"</span>/></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">uses-permission</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"android.permission.MOUNT_UNMOUNT_FILESYSTEMS"</span>/></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"></ul>

布局比较简单,就是一个下载按钮(忽略):

广播动态注册:

IntentFilter intentFilter = new IntentFilter();intentFilter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);getActivity().registerReceiver(receiver, intentFilter);
按钮点击事件

/**创建request对象*/DownloadManager.Request request = new DownloadManager.Request(Uri.parse("http://dingphone.ufile.ucloud.com.cn/apk/guanwang/time2plato.apk"));request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION);request.setTitle("apk下载");request.setDescription("我的app正在下载");request.setAllowedOverMetered(false);//不允许漫游request.allowScanningByMediaScanner();//创建下载文件目录request.setDestinationInExternalFilesDir(getContext(), Environment.DIRECTORY_DOWNLOADS, "my.apk");manger = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);manger.enqueue(request);//manager.remove(id);//下载任务和已经下载的文件同时删除
广播接收者

class DownLoadCompleteReceiver extends BroadcastReceiver {    /**     * 取得系统服务后,调用downloadmanager对象的enqueue方法进行下载,此方法返回一个编号用于标示此下载任务     */    @Override    public void onReceive(Context context, Intent intent) {        int id = (int) intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);        DownloadManager.Query query = new DownloadManager.Query();        query.setFilterById(id);        Cursor cursor = manger.query(query);        if (cursor != null) {            cursor.moveToNext();            String path = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));            Intent install = new Intent(Intent.ACTION_VIEW);            install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            install.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");            startActivity(install);        }    }


0 0
原创粉丝点击