版本更新
来源:互联网 发布:被淘宝托管骗了怎么办 编辑:程序博客网 时间:2024/06/13 22:52
首先呢,我们是应该在用户登录后,在首页执行检查版本信息的操作,具体是,获取到本地的版本号后,提交给服务器进行判断,然后后台来告诉我们当前版本是否为最新版本,紧接着我们拿到下载地址,执行下载的操作,具体的可以使用输入输出流来对文件进行存储和读取,最后我们将下载好的文件,调用系统的安装界面,进行安装,自此我们的更新操作全部完成。
首先记得添加权限
布局比较简单,就是一个下载按钮(忽略):
广播动态注册:
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