3、服務器端和客戶端設計
- 服務器端設計:
設計方法應該有不少,下面介紹個人一種方法:java
- a.首先在服務器項目下創建一個文件夾來存放APK安裝文件:
- b.其次在src下創建一個資源文件,apkVersion.properties,屬性定義以下:
- apkVersion=1 存版本號apkSize=550kb 大小apkPath=http://xx8080/srv/apk/Demo.apk 升級文件
定義類:UpdateApkServlet.javaandroid
-
- static {
- Properties ppt = new Properties();
- try {
- ppt.load(UpdateApkServlet.class .getResourceAsStream("/apkVersion.properties"));
- apkVersion = ppt.getProperty("apkVersion");
- apkSize = ppt.getProperty("apkSize");
- apkPath = ppt.getProperty("apkPath");
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
獲取資源,而後生成JSON字串返回客戶端處理。 注:當客戶端版本有更新,服務器端只要把APK文件拷貝到APK目錄,而後更新apkVersion.properties文件中的信息就能夠了,切記。服務器
客戶端設計:
- 一、 客戶端首先獲取服務器的版本信息(http方式獲取)。
- 二、 如何獲取本地客戶端的版本信息 以下參考代碼:
-
-
-
-
- private int getAPKVersion(){
-
- int sdcardVersion = 0;
- String apkFilePath="sdcard/demo.apk";
- PackageManager pm = getPackageManager();
- PackageInfo info = pm.getPackageArchiveInfo(apkFilePath, PackageManager.GET_ACTIVITIES);
- if(info != null){
- sdcardVersion=info.versionCode;
- Log.v(TAG, "Version="+sdcardVersion);
- }
- return sdcardVersion;
- }
- 三、 版本比較,若是版本相同,則不執行更新,不一樣才進行更新操做。 這裏插入客戶端版本設置介紹: 客戶端版本設置在AndroidManifest.xml文件中,裏面有兩個屬性可進行版本信息設置, android:versionCode="1" 版本號 android:versionName="1.1" 版本名稱 這個版本號須要和服務器端對應。
- 四、 須要的權限設置
- Sdcard訪問權限: uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
- 訪問網絡權限: uses-permission android:name="android.permission.INTERNET"
- 五、 更新安裝 當用戶點擊應用時執行檢查更新。相關代碼參考:
//彈出框提示網絡
- public Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- Dialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("系統更新").setMessage("發現新版本,請更新!")
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- pBar = new ProgressDialog(MainActivity.this);
- pBar.setTitle("正在下載");pBar.setMessage("請稍候...");
- pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);downFile(apkPath);}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
-
-
- dialog.show();
- }
- };
//下載app
-
-
-
-
- public void downFile(final String url) {
- pBar.show();
- new Thread() {
- public void run() {
- HttpClient client = new DefaultHttpClient();
-
- urlHttpGet get = new HttpGet(url);
- HttpResponse response;
- try {
- response = client.execute(get);
- HttpEntity entity = response.getEntity();
- long length = entity.getContentLength();
- InputStream is = entity.getContent();
- FileOutputStream fileOutputStream = null;
- if (is != null) {
- File file = new File(Environment.getExternalStorageDirectory(),"demo.apk");
- fileOutputStream = new FileOutputStream(file);
- byte[] buf = new byte[1024];
- int ch = -1;
- int count = 0;
- while ((ch = is.read(buf)) != -1) {
-
- fileOutputStream.write(buf, 0, ch);
- count += ch;if (length > 0) {}
- }
- }
- fileOutputStream.flush();
- if (fileOutputStream != null) {
- fileOutputStream.close();
- }
- down();
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }.start();
- }
- public void down() {
- handler.post(new Runnable()
- {
- public void run() {
- pBar.cancel();
- update();
- }});
- }
//更新升級ide
- public void update() {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.fromFile(new File("/sdcard/demo.apk")),"application/vnd.android.package-archive");
- startActivity(intent);
- }
結束,供參考。post