android 自动更新 服务端,搭建android版本更新服务器使用android系统自带的DownloadManager下载文件...
這幾天想自己做一個(gè)文件更新的功能,但是由于不知道怎樣寫(xiě)服務(wù)端,所以一直沒(méi)有去做,后來(lái)發(fā)現(xiàn)原來(lái)服務(wù)端編寫(xiě)簡(jiǎn)直是太簡(jiǎn)單了,所以今天就實(shí)現(xiàn)了
版本更新的這樣一個(gè)功能。
一搭建版本更新服務(wù)器:
搭建這個(gè)一個(gè)服務(wù)器其實(shí)很簡(jiǎn)單,或者說(shuō)根本就談不上什么搭建。就是將你要更新的文件以及這個(gè)更新文件的配置文件放在Tomcat目錄下(也可以是其他
的服務(wù)器,如:nigix),保存這個(gè)配置文件的路徑,將你所要更新文件的信息,如:版本,文件路徑,文件名等等保存在配置文件中,然后在客戶(hù)端根據(jù)你
所保存的配置文件的路徑,讀取配置文件的信息,根據(jù)配置文件中的信息去決定是否要更新。具體步驟如下:
1、搭建Tomcat服務(wù)器,配置好能夠正常運(yùn)行(這里不詳細(xì)寫(xiě)了,網(wǎng)上教程太多了)
2、編寫(xiě)配置xml文件,我的文件信息如下:
ITalkie_picc.apk 23 http://192.168.1.101:8080/app/ITalkie_picc.apk
這個(gè)文件中的內(nèi)容可以根據(jù)自己的需要去編寫(xiě),前面的標(biāo)識(shí)符如:name,url,等可以自定義。
3、在tomcat-6.0.37\webapps路徑下新建一個(gè)文件夾app,將你的配置文件以及要下載更新的文件(我這里要更新的文件為ITalkie_picc.apk)放入這個(gè)新
建的文件中。
4、啟動(dòng)Tomcat,在瀏覽器輸入:http://192.168.1.101:8080/app/update.xml?看是否能夠正常加載xml中的信息,在瀏覽其中輸入:
http://192.168.1.101:8080/app/ITalkie_picc.apk,看是否能夠正常下載你所要更新的文件。如果一切正常,那么你所想的服務(wù)端便編寫(xiě)好了,真的很簡(jiǎn)單
這里記住,在瀏覽器中輸入的地址不必帶有Tomcat的文件路徑如:你在webapps文件下創(chuàng)建app文件,在瀏覽器輸入地址時(shí)不必帶有webapps名字,只
需要輸入你的文件在app下的路徑即可。
二客戶(hù)端更新功能:
主要有以下幾個(gè)步驟:
1、在更新前要檢查網(wǎng)絡(luò)情況
2、根據(jù)保存的xml路徑,解析xml文件,獲取xml中的信息,根據(jù)這個(gè)信息來(lái)決定是否下載更新
3、如果決定下載更新,那么啟動(dòng)系統(tǒng)的download服務(wù),設(shè)置好相關(guān)信息
4、下載完成后,啟動(dòng)系統(tǒng)的安裝界面讓用戶(hù)選擇安裝。
下面放上主要代碼:
一設(shè)置系統(tǒng)DownloadManager的相關(guān)信息:
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);//設(shè)置是否顯示下載的notification request.setTitle(name); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(path, 'ITalkie_picc.apk'); //設(shè)置下載文件的存放外部路徑以及文件名,如果下載的是音樂(lè)等文件,這樣設(shè)置能夠讓其他音樂(lè)掃描到。 long id = download.enqueue(request);//將請(qǐng)求放入下載請(qǐng)求隊(duì)列,并返回標(biāo)志id SharedPreferences spf = context.getSharedPreferences('download', Activity.MODE_PRIVATE); SharedPreferences.Editor editor = spf.edit(); editor.putLong('download_id', id);//保存下載ID editor.commit(); } }}
二編寫(xiě)下載完成后的BroadCastReceive,自動(dòng)彈出安裝界面:
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', '收到數(shù)據(jù)'); 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路徑,而不是這個(gè)apk的路徑,所以要將uri轉(zhuǎn)化成文件的路徑 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 ; } // 通過(guò)Intent安裝apk文件,自動(dòng)打開(kāi)安裝界面 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中啟動(dòng)activity,所以啟動(dòng)方式必須設(shè)置為FLAG_ACTIVITY_NEW_TASK context.startActivity(intent); }}
這樣下載更新的功能便做完了,完整的代碼我會(huì)上傳到github上,需要的可以去pull下來(lái)。https://github.com/lonuery/DownloadUpdate.git
總結(jié)
以上是生活随笔為你收集整理的android 自动更新 服务端,搭建android版本更新服务器使用android系统自带的DownloadManager下载文件...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 100个人搬100块砖C语言,姓氏名人故
- 下一篇: android 购物车抛物线,添加到购物