安卓实现在线更新

来源:互联网 发布:上海知柚公司官网图片 编辑:程序博客网 时间:2024/05/01 16:05

////////////////////2016/04/25//////////////////////

///////////////////by XBW///////////////////////////

//////////////////环境 eclipse api22///////////

做点贡献,发几个小demo,每次启动某个app都会遇到app升级,这是怎么实现的呢,先上图吧,

有图有真相,

这截图真大,没睡了,


结构。

Config.java

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. package com.example.update;  
  2.   
  3. public class Config {  
  4. public static String mysql_url_update="http://127.0.0.1/androidapp/appupdate/update.txt";//服务器端更新url;  
  5. }  

FileUtil.java

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. package com.example.update;  
  2. import java.io.File;  
  3. import java.io.IOException;  
  4. import android.os.Environment;  
  5.   
  6. public class FileUtil {  
  7.       
  8.     public static File updateDir = null;  
  9.     public static File updateFile = null;  
  10.     public static final String Application = "konkaUpdateApplication";  
  11.       
  12.     public static boolean isCreateFileSucess;  
  13.     public static void createFile(String app_name) {  
  14.           
  15.         if (android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())) {  
  16.             isCreateFileSucess = true;  
  17.               
  18.             updateDir = new File(Environment.getExternalStorageDirectory()+ "/" +Application +"/");  
  19.             updateFile = new File(updateDir + "/" + app_name + ".apk");  
  20.   
  21.             if (!updateDir.exists()) {  
  22.                 updateDir.mkdirs();  
  23.             }  
  24.             if (!updateFile.exists()) {  
  25.                 try {  
  26.                     updateFile.createNewFile();  
  27.                 } catch (IOException e) {  
  28.                     isCreateFileSucess = false;  
  29.                     e.printStackTrace();  
  30.                 }  
  31.             }  
  32.   
  33.         }else{  
  34.             isCreateFileSucess = false;  
  35.         }  
  36.     }  
  37. }  


GetVersion.java

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. package com.example.update;  
  2.   
  3. import android.content.Context;  
  4. import android.content.pm.PackageInfo;  
  5. import android.content.pm.PackageManager;  
  6. import android.content.pm.PackageManager.NameNotFoundException;  
  7.   
  8. public class GetVersion {  
  9.   
  10.     // 获取当前版本的版本号  
  11.         public String getVersion(Context context) {  
  12.             try {  
  13.                 PackageManager packageManager = context.getPackageManager();  
  14.                 PackageInfo packageInfo = packageManager.getPackageInfo(  
  15.                         context.getPackageName(), 0);  
  16.                 return packageInfo.versionName;  
  17.             } catch (NameNotFoundException e) {  
  18.                 e.printStackTrace();  
  19.                 return "版本号未知";  
  20.             }  
  21.         }  
  22. }  


IsNeedUpdate.java

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. package com.example.update;  
  2.   
  3. import android.content.Context;  
  4.   
  5. public class IsNeedUpdate {  
  6.   
  7.     private UpdateInfo info;  
  8.     public IsNeedUpdate(Context context){  
  9.         UpdateInfoService updateInfoService = new UpdateInfoService(  
  10.                 context);  
  11.         try {  
  12.             info = updateInfoService.getUpDateInfo();  
  13.         } catch (Exception e) {  
  14.             // TODO 自动生成�? catch �?  
  15.             e.printStackTrace();  
  16.         }  
  17.     }  
  18.     public boolean isNeedUpdate(Context context) {  
  19.          GetVersion version=new GetVersion();  
  20.          //版本更新检测使用浮点型存在小版本问题,浮点型不能识别v 1.2.1这种小版本  
  21.          //double webVersion=Double.parseDouble(info.getVersion());  
  22.         // double localVersion=Double.parseDouble(version.getVersion(context));  
  23.          //采用比较字典序大小检测版本更新  
  24.         if (info.getVersion().compareTo(version.getVersion(context))>0) {  
  25.             return true;  
  26.         } else {  
  27.             return false;  
  28.         }  
  29.     }  
  30.     public String getDescribe(){  
  31.           
  32.         return info.getDescription();  
  33.     }  
  34.     public String getUrl(){  
  35.           
  36.         return info.getUrl();  
  37.     }  
  38. }  


ShowUpdateDialog.java

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. package com.example.update;  
  2.   
  3. import android.app.AlertDialog;  
  4. import android.app.Dialog;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.os.Environment;  
  8. import android.text.method.ScrollingMovementMethod;  
  9. import android.util.Log;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.ImageButton;  
  15. import android.widget.RelativeLayout;  
  16. import android.widget.TextView;  
  17. import android.widget.Toast;  
  18.   
  19. public class ShowUpdateDialog{  
  20.     private UpdateInfo info;  
  21.     //更新窗口  
  22.     public Dialog showUpdateDialog(final Context context,String msg,final String url) {  
  23.         LayoutInflater layoutInflater =LayoutInflater.from(context);  
  24.         RelativeLayout layout = (RelativeLayout)layoutInflater.inflate(R.layout.dialog, null );  
  25.         final Dialog dialog = new AlertDialog.Builder(context).create();  
  26.         dialog.show();  
  27.         dialog.getWindow().setContentView(layout);  
  28.         TextView tex=(TextView)layout.findViewById(R.id.dialog_text);  
  29.         TextView tex1=(TextView)layout.findViewById(R.id.textView_title);  
  30.         tex.setMovementMethod(ScrollingMovementMethod.getInstance());   
  31.         tex.setText(msg);  
  32.         tex1.setText("更新提示");  
  33.          //确定按钮  
  34.          Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);  
  35.          btnOK.setOnClickListener(new OnClickListener() {           
  36.            @Override   
  37.            public void onClick(View v) {   
  38.                if (Environment.getExternalStorageState().equals(  
  39.                         Environment.MEDIA_MOUNTED)) {  
  40.                         //services.service();  
  41.                    Intent intent = new Intent(context,UpdateServices.class);  
  42.                    intent.putExtra("Key_App_Name",context.getString(R.string.app_name));  
  43.                    intent.putExtra("Key_Down_Url",url);       
  44.                    context.startService(intent);  
  45.                 } else {  
  46.                     Toast.makeText(context, "SD卡不可用,请插入SD卡",  
  47.                             Toast.LENGTH_SHORT).show();  
  48.                 }  
  49.                dialog.dismiss();      
  50.            }  
  51.          });       
  52.          //关闭按钮  
  53.          ImageButton btnClose = (ImageButton) layout.findViewById(R.id.dialog_close);  
  54.          btnClose.setOnClickListener(new OnClickListener() {                
  55.            @Override  
  56.            public void onClick(View v) {  
  57.               dialog.dismiss();            
  58.            }  
  59.          });  
  60.          return dialog;  
  61.      }  
  62.     //不更新窗口  
  63.     public Dialog showDialog(final Context context) {  
  64.         LayoutInflater layoutInflater =LayoutInflater.from(context);  
  65.         RelativeLayout layout = (RelativeLayout)layoutInflater.inflate(R.layout.dialog, null );  
  66.         final Dialog dialog = new AlertDialog.Builder(context).create();  
  67.         dialog.show();  
  68.         dialog.getWindow().setContentView(layout);  
  69.         TextView tex=(TextView)layout.findViewById(R.id.dialog_text);  
  70.         TextView tex1=(TextView)layout.findViewById(R.id.textView_title);  
  71.         GetVersion version=new GetVersion();  
  72.         tex.setText("您使用的是最新版:"+version.getVersion(context)+"版本");  
  73.         tex1.setText("更新提示");  
  74.         Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);  
  75.         btnOK.setVisibility(View.INVISIBLE);  
  76.       //关闭按钮  
  77.          ImageButton btnClose = (ImageButton) layout.findViewById(R.id.dialog_close);  
  78.          btnClose.setOnClickListener(new OnClickListener() {                
  79.            @Override  
  80.            public void onClick(View v) {  
  81.               dialog.dismiss();            
  82.            }  
  83.          });  
  84.          return dialog;  
  85.     }  
  86.   
  87. }  


UpdateInfo.java

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. package com.example.update;  
  2.   
  3. public class UpdateInfo  
  4. {  
  5.         private String version;  
  6.         private String description;  
  7.         private String url;  
  8.         public String getVersion()  
  9.         {  
  10.                 return version;  
  11.         }  
  12.         public void setVersion(String version)  
  13.         {  
  14.                 this.version = version;  
  15.         }  
  16.         public String getDescription()  
  17.         {  
  18.                 return description;  
  19.         }  
  20.         public void setDescription(String description)  
  21.         {  
  22.                 this.description = description;  
  23.         }  
  24.         public String getUrl()  
  25.         {  
  26.                 return url;  
  27.         }  
  28.         public void setUrl(String url)  
  29.         {  
  30.                 this.url = url;  
  31.         }  
  32. }  


UpdateInfoService.java

[java] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. package com.example.update;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.InputStreamReader;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7. import android.content.Context;  
  8.   
  9. public class UpdateInfoService {  
  10.     public UpdateInfoService(Context context) {  
  11.     }  
  12.   
  13.     public UpdateInfo getUpDateInfo() throws Exception {  
  14.         String path = Config.mysql_url_update;  
  15.         StringBuffer sb = new StringBuffer();  
  16.         String line = null;  
  17.         BufferedReader reader = null;  
  18.         UpdateInfo updateInfo = new UpdateInfo();  
  19.         try {  
  20.             // 创建�?个url对象  
  21.             URL url = new URL(path);  
  22.             // 通過url对象,创建一个HttpURLConnection对象(连接)  
  23.             HttpURLConnection urlConnection = (HttpURLConnection) url  
  24.                     .openConnection();  
  25.             // 通过HttpURLConnection对象,得到InputStream  
  26.             reader = new BufferedReader(new InputStreamReader(  
  27.                     urlConnection.getInputStream()));  
  28.             // 使用io流读取文�?  
  29.             while ((line = reader.readLine()) != null) {  
  30.                 sb.append(line);  
  31.                   
  32.                   
  33.             }  
  34.         } catch (Exception e) {  
  35.             e.printStackTrace();  
  36.         } finally {  
  37.             try {  
  38.                 if (reader != null) {  
  39.                     reader.close();  
  40.                 }  
  41.             } catch (Exception e) {  
  42.                 e.printStackTrace();  
  43.             }  
  44.         }  
  45.       
  46.         String info = sb.toString();  
  47.         updateInfo.setVersion(info.split("&")[1]);  
  48.         updateInfo.setDescription(info.split("&")[2]);  
  49.         updateInfo.setUrl(info.split("&")[3]);  
  50.         return updateInfo;  
  51.     }  
  52.   
  53. }  


才发现csdn博客还有长度限制,发了好几次都被截断了,这样吧,

直接传送demo,或者去我小站看一下吧

demo传送门

小站

小站demo下载不需要积分哦

0 0