当前位置: 代码迷 >> Android >> Android服务器端跟客户端设计APK升级
  详细解决方案

Android服务器端跟客户端设计APK升级

热度:62   发布时间:2016-05-01 16:59:57.0
Android服务器端和客户端设计APK升级

服务器端设计:?
设计方法应该有很多,下面介绍我的一种方法:
a.首先在服务器项目下建立一个文件夹来存放APK安装文件:?
b.其次在src下建立一个资源文件,apkVersion.properties,属性定义如下写道?

view plaincopy to clipboardprint?
apkVersion=1 存版本号
apkSize=550kb 大小
apkPath=http://xx8080/srv/apk/Demo.apk 升级文件
apkVersion=1 存版本号
apkSize=550kb 大小
apkPath=http://xx8080/srv/apk/Demo.apk 升级文件

c.定义一个servlet来获取资源中的信息:?
定义类:UpdateApkServlet.java?

?

view plaincopy to clipboardprint?//获取资源文件信息   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();   }   }   //获取资源文件信息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文件中的信息就可以了,切记。?

客户端设计:?
1、 客户端首先获取服务器的版本信息(http方式获取)。?
2、 如何获取本地客户端的版本信息 如下参考代码:?

?

view plaincopy to clipboardprint?/**  * 得到本地应用的版本信息   * @return  */  private int getAPKVersion(){       //APK版本判断       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;   }  /**  * 得到本地应用的版本信息   * @return  */private int getAPKVersion(){  //APK版本判断  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;} 

?3、 版本比较,如果版本相同,则不执行更新,不同才进行更新操作。 这里插入客户端版本设置介绍: 客户端版本设置在AndroidManifest.xml文件中,里面有两个属性可进行版本信息设置, android:versionCode="1" 版本号 android:versionName="1.1" 版本名称 这个版本号需要和服务器端对应。?

4、 需要的权限设置?

?

view plaincopy to clipboardprint?
Sdcard访问权限: uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
访问网络权限: uses-permission android:name="android.permission.INTERNET"
Sdcard访问权限: uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
访问网络权限: uses-permission android:name="android.permission.INTERNET"

?5、 更新安装 当用户点击应用时执行检查更新。相关代码参考:?

?

  //弹出框提示view plaincopy to clipboardprint?public Handler handler = new Handler() {       public void handleMessage(Message msg) {           super.handleMessage(msg);                      Dialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("系统更新").setMessage("发现新版本,请更新!")            // 设置内容           .setPositiveButton("确定",// 设置确定按钮                   new DialogInterface.OnClickListener() {                       @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) {                   // 点击"取消"按钮操作               }           }).create();// 创建       // 显示对话框               dialog.show();           }       };  public Handler handler = new Handler() {  public void handleMessage(Message msg) {   super.handleMessage(msg);           Dialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("系统更新").setMessage("发现新版本,请更新!")    // 设置内容   .setPositiveButton("确定",// 设置确定按钮     new DialogInterface.OnClickListener() {      @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) {     // 点击"取消"按钮操作    }   }).create();// 创建  // 显示对话框    dialog.show();         }  }; //下载view plaincopy to clipboardprint?/**  * DOWNLOAD APK FILE BY URL  * @param url  */  public void downFile(final String url) {       pBar.show();       new Thread() {           public void run() {               HttpClient client = new DefaultHttpClient();               // params[0]代表连接的url               HttpGet 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) {                           // baos.write(buf, 0, ch);                           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();           }       });   }  /**  * DOWNLOAD APK FILE BY URL  * @param url  */public void downFile(final String url) {  pBar.show();  new Thread() {   public void run() {    HttpClient client = new DefaultHttpClient();    // params[0]代表连接的url    HttpGet 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) {       // baos.write(buf, 0, ch);       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();   }  });}  //更新升级view plaincopy to clipboardprint?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);   }  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);}

?

原文地址:http://blog.csdn.net/muyu114/article/details/6623509

?

?

  相关解决方案