分享

搭建android版本更新服务器使用android系统自带的DownloadManager下载文件

 guitarhua 2017-06-08

这几天想自己做一个文件更新的功能,但是由于不知道怎样写服务端,所以一直没有去做,后来发现原来服务端编写简直是太简单了,所以今天就实现了

版本更新的这样一个功能。

一搭建版本更新服务器:

搭建这个一个服务器其实很简单,或者说根本就谈不上什么搭建。就是将你要更新的文件以及这个更新文件的配置文件放在Tomcat目录下(也可以是其他

的服务器,如:nigix),保存这个配置文件的路径,将你所要更新文件的信息,如:版本,文件路径,文件名等等保存在配置文件中,然后在客户端根据你

所保存的配置文件的路径,读取配置文件的信息,根据配置文件中的信息去决定是否要更新。具体步骤如下:

1、搭建Tomcat服务器,配置好能够正常运行(这里不详细写了,网上教程太多了)

2、编写配置xml文件,我的文件信息如下:

ITalkie_picc.apk 23 http://192.168.1.101:8080/app/ITalkie_picc.apk

这个文件中的内容可以根据自己的需要去编写,前面的标识符如:name,url,等可以自定义。

3、在tomcat-6.0.37\webapps路径下新建一个文件夹app,将你的配置文件以及要下载更新的文件(我这里要更新的文件为ITalkie_picc.apk)放入这个新

建的文件中。

4、启动Tomcat,在浏览器输入:http://192.168.1.101:8080/app/update.xml 看是否能够正常加载xml中的信息,在浏览其中输入:

http://192.168.1.101:8080/app/ITalkie_picc.apk,看是否能够正常下载你所要更新的文件。如果一切正常,那么你所想的服务端便编写好了,真的很简单

这里记住,在浏览器中输入的地址不必带有Tomcat的文件路径如:你在webapps文件下创建app文件,在浏览器输入地址时不必带有webapps名字,只

需要输入你的文件在app下的路径即可。

二客户端更新功能:

主要有以下几个步骤:

1、在更新前要检查网络情况

2、根据保存的xml路径,解析xml文件,获取xml中的信息,根据这个信息来决定是否下载更新

3、如果决定下载更新,那么启动系统的download服务,设置好相关信息

4、下载完成后,启动系统的安装界面让用户选择安装。

下面放上主要代码:

一设置系统DownloadManager的相关信息:

public class SystemDownLoad { Context context; String url,path,name; public SystemDownLoad(Context context,String url,String name,String path){ this.context = context; this.url = url; this.name = name; this.path = path; } public void downLoad(){ if(url!=null && name!=null && path!=null){ DownloadManager download = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE); Uri uri = Uri.parse(url); Request request = new Request(uri); request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE|DownloadManager.Request.NETWORK_WIFI); request.setVisibleInDownloadsUi(true);//设置是否显示下载的notification request.setTitle(name); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(path, 'ITalkie_picc.apk'); //设置下载文件的存放外部路径以及文件名,如果下载的是音乐等文件,这样设置能够让其他音乐扫描到。 long id = download.enqueue(request);//将请求放入下载请求队列,并返回标志id SharedPreferences spf = context.getSharedPreferences('download', Activity.MODE_PRIVATE); SharedPreferences.Editor editor = spf.edit(); editor.putLong('download_id', id);//保存下载ID editor.commit(); } }}

二编写下载完成后的BroadCastReceive,自动弹出安装界面:

public class DownLoadReceive extends BroadcastReceiver{ DownloadManager downloadManager; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)){ Log.i('DownLoadReceive', '收到数据'); long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0); downloadManager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE); SharedPreferences spf = context.getSharedPreferences('download', Activity.MODE_PRIVATE); long download_id = spf.getLong('download_id',0); Query query = new Query(); query.setFilterById(id); Cursor cursor = downloadManager.query(query); String path =null; if(cursor.moveToFirst()){ int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS); if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(columnIndex)) { String uriString = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); //上面获取的uriString 是所下载的apk uri路径,而不是这个apk的路径,所以要将uri转化成文件的路径 Uri uri = Uri.parse(uriString); path = uri.getPath(); } cursor.close(); } if(id == download_id && path !=null){ installApk(path,context); } } } private void installApk(String path,Context context){ File file = new File(path); if(!file.exists()){ Log.i('DownLoadReceive', '文件不存在'); return ; } // 通过Intent安装apk文件,自动打开安装界面 Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), 'application/vnd.android.package-archive'); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //由于是在BroadcastReceive中启动activity,所以启动方式必须设置为FLAG_ACTIVITY_NEW_TASK context.startActivity(intent); }}


这样下载更新的功能便做完了,完整的代码我会上传到github上,需要的可以去pull下来。https://github.com/lonuery/DownloadUpdate.git


 

 

 

 

 

 

 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多