android版本更新功能

来源:互联网 发布:hksystem是什么软件 编辑:程序博客网 时间:2024/05/17 22:27

源码下载地址:http://download.csdn.net/download/csdn576038874/9526085

 

1、检测是否有新版本

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <pre class="java" name="code">public class MainActivity extends Activity {  
  2.   
  3.     private Button button;  
  4.       
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.           
  10.         button = (Button) findViewById(R.id.button1);  
  11.           
  12.         button.setOnClickListener(new OnClickListener() {  
  13.               
  14.             @Override  
  15.             public void onClick(View v) {  
  16.                 //本地测试检测是否有新版本发布  
  17.                 UpdateVersionUtil.localCheckedVersion(MainActivity.this,new UpdateListener() {  
  18.                       
  19.                     @Override  
  20.                     public void onUpdateReturned(int updateStatus, VersionInfo versionInfo) {  
  21.                         //判断回调过来的版本检测状态  
  22.                         switch (updateStatus) {  
  23.                         case UpdateStatus.YES:  
  24.                             //弹出更新提示  
  25.                             UpdateVersionUtil.showDialog(MainActivity.this,versionInfo);  
  26.                             break;  
  27.                         case UpdateStatus.NO:  
  28.                             //没有新版本  
  29.                             ToastUtils.showToast(getApplicationContext(), "已经是最新版本了!");  
  30.                             break;  
  31.                         case UpdateStatus.NOWIFI:  
  32.                             //当前是非wifi网络  
  33.                             ToastUtils.showToast(getApplicationContext(), "只有在wifi下更新!");  
  34. //                          DialogUtils.showDialog(MainActivity.this, "温馨提示","当前非wifi网络,下载会消耗手机流量!", "确定", "取消",new DialogOnClickListenner() {  
  35. //                              @Override  
  36. //                              public void btnConfirmClick(Dialog dialog) {  
  37. //                                  dialog.dismiss();  
  38. //                                  //点击确定之后弹出更新对话框  
  39. //                                  UpdateVersionUtil.showDialog(SystemActivity.this,versionInfo);  
  40. //                              }  
  41. //                                
  42. //                              @Override  
  43. //                              public void btnCancelClick(Dialog dialog) {  
  44. //                                  dialog.dismiss();  
  45. //                              }  
  46. //                          });  
  47.                             break;  
  48.                         case UpdateStatus.ERROR:  
  49.                             //检测失败  
  50.                             ToastUtils.showToast(getApplicationContext(), "检测失败,请稍后重试!");  
  51.                             break;  
  52.                         case UpdateStatus.TIMEOUT:  
  53.                             //链接超时  
  54.                             ToastUtils.showToast(getApplicationContext(), "链接超时,请检查网络设置!");  
  55.                             break;  
  56.                         }  
  57.                     }  
  58.                 });  
  59.                   
  60.                 /** 
  61.                  * //访问服务器 试检测是否有新版本发布 
  62.                 UpdateVersionUtil.localCheckedVersion(MainActivity.this,new UpdateListener() { 
  63.                      
  64.                     @Override 
  65.                     public void onUpdateReturned(int updateStatus, VersionInfo versionInfo) { 
  66.                         //判断回调过来的版本检测状态 
  67.                         switch (updateStatus) { 
  68.                         case UpdateStatus.YES: 
  69.                             //弹出更新提示 
  70.                             UpdateVersionUtil.showDialog(MainActivity.this,versionInfo); 
  71.                             break; 
  72.                         case UpdateStatus.NO: 
  73.                             //没有新版本 
  74.                             ToastUtils.showToast(getApplicationContext(), "已经是最新版本了!"); 
  75.                             break; 
  76.                         case UpdateStatus.NOWIFI: 
  77.                             //当前是非wifi网络 
  78.                             ToastUtils.showToast(getApplicationContext(), "只有在wifi下更新!"); 
  79. //                          DialogUtils.showDialog(MainActivity.this, "温馨提示","当前非wifi网络,下载会消耗手机流量!", "确定", "取消",new DialogOnClickListenner() { 
  80. //                              @Override 
  81. //                              public void btnConfirmClick(Dialog dialog) { 
  82. //                                  dialog.dismiss(); 
  83. //                                  //点击确定之后弹出更新对话框 
  84. //                                  UpdateVersionUtil.showDialog(SystemActivity.this,versionInfo); 
  85. //                              } 
  86. //                               
  87. //                              @Override 
  88. //                              public void btnCancelClick(Dialog dialog) { 
  89. //                                  dialog.dismiss(); 
  90. //                              } 
  91. //                          }); 
  92.                             break; 
  93.                         case UpdateStatus.ERROR: 
  94.                             //检测失败 
  95.                             ToastUtils.showToast(getApplicationContext(), "检测失败,请稍后重试!"); 
  96.                             break; 
  97.                         case UpdateStatus.TIMEOUT: 
  98.                             //链接超时 
  99.                             ToastUtils.showToast(getApplicationContext(), "链接超时,请检查网络设置!"); 
  100.                             break; 
  101.                         } 
  102.                     } 
  103.                 }); 
  104.                  */  
  105.             }  
  106.         });  
  107.     }  
  108. }  

2、版本检测的工具类
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <pre class="java" name="code">/** 
  2.  *  
  3.  * @author  wenjie 
  4.  *  版本更新的工具类 
  5.  */  
  6. public class UpdateVersionUtil{  
  7.       
  8.     /** 
  9.      * 接口回调 
  10.      * @author wenjie 
  11.      * 
  12.      */  
  13.     public interface UpdateListener{  
  14.         void onUpdateReturned(int updateStatus,VersionInfo versionInfo);  
  15.     }  
  16.       
  17.     public UpdateListener updateListener;  
  18.       
  19.     public void setUpdateListener(UpdateListener updateListener) {  
  20.         this.updateListener = updateListener;  
  21.     }  
  22.       
  23.     /** 
  24.      * 网络测试 检测版本 
  25.      * @param context 上下文 
  26.      */  
  27.     public static void checkVersion(final Context context,final UpdateListener updateListener){  
  28.         HttpRequest.get(ServerReqAddress.UPDATA_VERSION_REQ, new RequestCallBackListener() {  
  29.               
  30.             @Override  
  31.             public void onSuccess(String resultData) {  
  32.                 try {  
  33.                     JSONObject jsonObject = JsonUtil.stringToJson(resultData);  
  34.                     JSONArray array = jsonObject.getJSONArray("data");  
  35.                     VersionInfo mVersionInfo = JsonUtil.jsonToBean(array.getJSONObject(0).toString(), VersionInfo.class);  
  36.                     int clientVersionCode = ApkUtils.getVersionCode(context);  
  37.                     int serverVersionCode = mVersionInfo.getVersionCode();  
  38.                     //有新版本  
  39.                     if(clientVersionCode < serverVersionCode){  
  40.                         int i = NetworkUtil.checkedNetWorkType(context);  
  41.                         if(i == NetworkUtil.NOWIFI){  
  42.                             updateListener.onUpdateReturned(UpdateStatus.NOWIFI,mVersionInfo);  
  43.                         }else if(i == NetworkUtil.WIFI){  
  44.                             updateListener.onUpdateReturned(UpdateStatus.YES,mVersionInfo);  
  45.                         }  
  46.                     }else{  
  47.                         //无新本  
  48.                         updateListener.onUpdateReturned(UpdateStatus.NO,null);  
  49.                     }  
  50.                 } catch (Exception e) {  
  51.                     e.printStackTrace();  
  52.                     updateListener.onUpdateReturned(UpdateStatus.ERROR,null);  
  53.                 }  
  54.             }  
  55.               
  56.             @Override  
  57.             public void onFailure(String error) {  
  58.                 updateListener.onUpdateReturned(UpdateStatus.TIMEOUT,null);  
  59.             }  
  60.         });  
  61.     }  
  62.       
  63.       
  64.     /** 
  65.      * 本地测试 
  66.      */  
  67.     public static void localCheckedVersion(final Context context,final UpdateListener updateListener){  
  68.         try {  
  69. //          JSONObject jsonObject = JsonUtil.stringToJson(resultData);  
  70. //          JSONArray array = jsonObject.getJSONArray("data");  
  71. //          VersionInfo mVersionInfo = JsonUtil.jsonToBean(array.getJSONObject(0).toString(), VersionInfo.class);  
  72.             VersionInfo mVersionInfo = new VersionInfo();  
  73.             mVersionInfo.setDownloadUrl("http://gdown.baidu.com/data/wisegame/57a788487345e938/QQ_358.apk");  
  74.             mVersionInfo.setVersionDesc("\n更新内容:\n1、增加xxxxx功能\n2、增加xxxx显示!\n3、用户界面优化!\n4、xxxxxx!");  
  75.             mVersionInfo.setVersionCode(2);  
  76.             mVersionInfo.setVersionName("v2020");  
  77.             mVersionInfo.setVersionSize("20.1M");  
  78.             mVersionInfo.setId("1");  
  79.             int clientVersionCode = ApkUtils.getVersionCode(context);  
  80.             int serverVersionCode = mVersionInfo.getVersionCode();  
  81.             //有新版本  
  82.             if(clientVersionCode < serverVersionCode){  
  83.                 int i = NetworkUtil.checkedNetWorkType(context);  
  84.                 if(i == NetworkUtil.NOWIFI){  
  85.                     updateListener.onUpdateReturned(UpdateStatus.NOWIFI,mVersionInfo);  
  86.                 }else if(i == NetworkUtil.WIFI){  
  87.                     updateListener.onUpdateReturned(UpdateStatus.YES,mVersionInfo);  
  88.                 }  
  89.             }else{  
  90.                 //无新本  
  91.                 updateListener.onUpdateReturned(UpdateStatus.NO,null);  
  92.             }  
  93.         } catch (Exception e) {  
  94.             e.printStackTrace();  
  95.             updateListener.onUpdateReturned(UpdateStatus.ERROR,null);  
  96.         }  
  97.     }  
  98.       
  99.       
  100.     /** 
  101.      * 弹出新版本提示 
  102.      * @param context 上下文 
  103.      * @param versionInfo 更新内容 
  104.      */  
  105.     public static void showDialog(final Context context,final VersionInfo versionInfo){  
  106.         final Dialog dialog = new AlertDialog.Builder(context).create();  
  107.         final File file = new File(SDCardUtils.getRootDirectory()+"/updateVersion/gdmsaec-app.apk");  
  108.         dialog.setCancelable(true);// 可以用“返回键”取消    
  109.         dialog.setCanceledOnTouchOutside(false);//  
  110.         dialog.show();  
  111.         View view = LayoutInflater.from(context).inflate(R.layout.version_update_dialog, null);  
  112.         dialog.setContentView(view);  
  113.           
  114.         final Button btnOk = (Button) view.findViewById(R.id.btn_update_id_ok);  
  115.         Button btnCancel = (Button) view.findViewById(R.id.btn_update_id_cancel);  
  116.         TextView tvContent = (TextView) view.findViewById(R.id.tv_update_content);  
  117.         TextView tvUpdateTile = (TextView) view.findViewById(R.id.tv_update_title);  
  118.         final TextView tvUpdateMsgSize = (TextView) view.findViewById(R.id.tv_update_msg_size);  
  119.           
  120.         tvContent.setText(versionInfo.getVersionDesc());  
  121.         tvUpdateTile.setText("最新版本:"+versionInfo.getVersionName());  
  122.           
  123.         if(file.exists() && file.getName().equals("gdmsaec-app.apk")){  
  124.             tvUpdateMsgSize.setText("新版本已经下载,是否安装?");  
  125.         }else{  
  126.             tvUpdateMsgSize.setText("新版本大小:"+versionInfo.getVersionSize());  
  127.         }  
  128.           
  129.         btnOk.setOnClickListener(new OnClickListener() {  
  130.             @Override  
  131.             public void onClick(View v) {  
  132.                 dialog.dismiss();  
  133.                 if(v.getId() == R.id.btn_update_id_ok){  
  134.                     //新版本已经下载  
  135.                     if(file.exists() && file.getName().equals("gdmsaec-app.apk")){  
  136.                         Intent intent = ApkUtils.getInstallIntent(file);  
  137.                         context.startActivity(intent);  
  138.                     }else{  
  139.                         //没有下载,则开启服务下载新版本  
  140.                         Intent intent = new Intent(context,UpdateVersionService.class);  
  141.                         intent.putExtra("downloadUrl", versionInfo.getDownloadUrl());  
  142.                         context.startService(intent);  
  143.                     }  
  144.                 }  
  145.             }  
  146.         });  
  147.           
  148.         btnCancel.setOnClickListener(new OnClickListener() {  
  149.             @Override  
  150.             public void onClick(View v) {  
  151.                 dialog.dismiss();  
  152.             }  
  153.         });  
  154.     }  
  155.       
  156.     /** 
  157.      * 收起通知栏 
  158.      * @param context 
  159.      */  
  160.     public static void collapseStatusBar(Context context) {   
  161.         try{  
  162.             Object statusBarManager = context.getSystemService("statusbar");   
  163.             Method collapse;  
  164.             if (Build.VERSION.SDK_INT <= 16){  
  165.                 collapse = statusBarManager.getClass().getMethod("collapse");   
  166.             }else{   
  167.                 collapse = statusBarManager.getClass().getMethod("collapsePanels");   
  168.             }   
  169.             collapse.invoke(statusBarManager);  
  170.         }catch (Exception localException){   
  171.             localException.printStackTrace();  
  172.         }   
  173.     }  
  174. }  

3、版本检测的状态类
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <pre class="java" name="code">/** 
  2.  *  
  3.  * @author wenjie 
  4.  *  检测版本的状态类 
  5.  */  
  6. public interface UpdateStatus {  
  7.     /** 
  8.      * 没有新版本 
  9.      */  
  10.     public static int NO = 1;  
  11.       
  12.     /** 
  13.      * 有新版本 
  14.      */  
  15.     public static int YES = 2;  
  16.       
  17.     /** 
  18.      * 链接超时 
  19.      */  
  20.     public static int TIMEOUT = 3;  
  21.       
  22.     /** 
  23.      * 没有wifi 
  24.      */  
  25.     public static int NOWIFI = 4;  
  26.       
  27.     /** 
  28.      * 数据解析出错 
  29.      */  
  30.     public static int ERROR = -1;  
  31. }  

4、版本更新的service
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <pre class="java" name="code">/** 
  2.  *  
  3.  * @author wenjie 
  4.  *  下载新版本的服务类 
  5.  */  
  6. public class UpdateVersionService extends Service {  
  7.   
  8.       
  9.     private NotificationManager nm;  
  10.     private Notification notification;  
  11.     //标题标识  
  12.     private int titleId = 0;  
  13.     //安装文件  
  14.     private File updateFile;  
  15.       
  16.     private static HttpHandler<File> httpHandler;  
  17.     private HttpUtils httpUtils;  
  18.       
  19.     private long initTotal = 0;//文件的总长度  
  20.       
  21.     @Override  
  22.     public void onCreate() {  
  23.         super.onCreate();  
  24.           
  25.         httpUtils = new HttpUtils();  
  26.         updateFile = new File(SDCardUtils.getRootDirectory()+"/updateVersion/gdmsaec-app.apk");  
  27.           
  28.         nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
  29.         notification = new Notification();  
  30.         notification.icon = R.drawable.ic_launcher;  
  31.         notification.tickerText = "开始下载";  
  32.         notification.when = System.currentTimeMillis();  
  33.         notification.contentView = new RemoteViews(getPackageName(), R.layout.notifycation);  
  34.           
  35.     }  
  36.   
  37.     @Override  
  38.     public int onStartCommand(Intent intent, int flags, int startId) {  
  39.           
  40. //      VersionInfo versionInfo = (VersionInfo) intent.getSerializableExtra("versionInfo");  
  41. //      String url = versionInfo.getDownloadUrl();  
  42.         Bundle bundle = intent.getExtras();  
  43.         String url = bundle.getString("downloadUrl");  
  44.           
  45.         PreferenceUtils.setString(UpdateVersionService.this"apkDownloadurl", url);  
  46.           
  47.         nm.notify(titleId, notification);  
  48.         downLoadFile(url);  
  49.         return super.onStartCommand(intent, flags, startId);  
  50.     }  
  51.   
  52.       
  53.       
  54.     public void downLoadFile(String url){  
  55.           
  56.         httpHandler = httpUtils.download(url,updateFile.getAbsolutePath(), truefalsenew RequestCallBack<File>() {  
  57.               
  58.             @Override  
  59.             public void onSuccess(ResponseInfo<File> response) {  
  60.                 // 更改文字  
  61.                 notification.contentView.setTextViewText(R.id.msg, "下载完成!点击安装");  
  62. //                notification.contentView.setViewVisibility(R.id.btnStartStop, View.GONE);  
  63. //                notification.contentView.setViewVisibility(R.id.btnCancel,View.GONE);  
  64.                 // 发送消息  
  65.                 nm.notify(0, notification);  
  66.                 stopSelf();  
  67.                 //收起通知栏  
  68.                 UpdateVersionUtil.collapseStatusBar(UpdateVersionService.this);  
  69.                 //自动安装新版本  
  70.                 Intent installIntent = ApkUtils.getInstallIntent(updateFile);  
  71.                 startActivity(installIntent);  
  72.                   
  73.             }  
  74.               
  75.             @Override  
  76.             public void onFailure(HttpException error, String msg) {  
  77.                 //网络连接错误  
  78.                 if(error.getExceptionCode() == 0 ){  
  79.                     // 更改文字  
  80.                     notification.contentView.setTextViewText(R.id.msg, "网络异常!请检查网络设置!");  
  81.                 }else if(error.getExceptionCode() == 416){//文件已经下载完毕  
  82.                     // 更改文字  
  83.                     notification.contentView.setTextViewText(R.id.msg, "智慧海事");  
  84.                     // 更改文字  
  85.                     notification.contentView.setTextViewText(R.id.bartext, "检测到新版本已经下载完成,点击即安装!");  
  86.                     // 隐藏进度条  
  87.                     notification.contentView.setViewVisibility(R.id.progressBar1, View.GONE);  
  88.                       
  89.                     Intent intent = ApkUtils.getInstallIntent(updateFile);  
  90.                     PendingIntent pendingIntent = PendingIntent.getActivity(UpdateVersionService.this0, intent, 0);  
  91.                     notification.flags = Notification.FLAG_AUTO_CANCEL;//点击通知栏之后 消失  
  92.                     notification.contentIntent  = pendingIntent;//启动指定意图  
  93.                 }  
  94.                 // 发送消息  
  95.                 nm.notify(0, notification);  
  96.             }  
  97.   
  98.             @Override  
  99.             public void onLoading(long total, long current, boolean isUploading) {  
  100.                 if(initTotal == 0){//说明第一次开始下载  
  101.                     initTotal = total;  
  102.                 }  
  103.                   
  104.                 if(initTotal != total){//说明下载过程中暂停过,文件的总长度出现问题  就把初始的文件的长度赋值给他重新计算已经下载的比例  
  105.                     total = initTotal;  
  106.                 }  
  107.                   
  108.                 long l = current*100/total;  
  109.                 notification.contentView.setTextViewText(R.id.msg, "正在下载:智慧海事");  
  110.                 // 更改文字  
  111.                 notification.contentView.setTextViewText(R.id.bartext, l+ "%");  
  112.                 // 更改进度条  
  113.                 notification.contentView.setProgressBar(R.id.progressBar1, 100,(int)l, false);  
  114.                 // 发送消息  
  115.                 nm.notify(0, notification);  
  116.                   
  117. //              Intent intent = new Intent();  
  118. //              intent.setAction("cancel");  
  119. //              PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);  
  120. //              notification.contentView.setOnClickPendingIntent(R.id.btnStartStop, pendingIntent);  
  121.                   
  122.             }  
  123.   
  124.             @Override  
  125.             public void onStart() {  
  126.                 notification.contentView.setTextViewText(R.id.msg, "开始下载:智慧海事");  
  127.                 nm.notify(titleId, notification);  
  128.             }  
  129.               
  130.         });  
  131.     }  
  132.       
  133.       
  134.     public static HttpHandler<File> getHandler(){  
  135.         return httpHandler;  
  136.     }  
  137.       
  138.       
  139.     @Override  
  140.     public void onDestroy() {  
  141.         //下载完成时,清楚该通知,自动安装  
  142.         nm.cancel(titleId);  
  143.         System.out.println("UpdateVersionService----onDestroy");  
  144. //      try {  
  145. //          GdmsaecApplication.db.deleteAll(VersionInfo.class);  
  146. //      } catch (DbException e) {  
  147. //          e.printStackTrace();  
  148. //      }  
  149.         super.onDestroy();  
  150.     }  
  151.       
  152.     @Override  
  153.     public IBinder onBind(Intent intent) {  
  154.         return null;  
  155.     }  
  156.       
  157.       
  158. }  

5、吐司的工具类
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <pre class="java" name="code">/** 
  2.  * Toast工具箱  可防止用户多次点击之后 显示消息的时长太长  
  3.  */  
  4. public class ToastUtils {  
  5.       
  6.     private static String oldMsg;    
  7.     protected static Toast toast   = null;    
  8.     private static long oneTime=0;    
  9.     private static long twoTime=0;    
  10.     /** 
  11.      * 吐出一个显示时间较短的提示 
  12.      * @param context 上下文 
  13.      * @param s  文本内容 
  14.      */  
  15.     public static void showToast(Context context, String s){        
  16.         if(toast==null){     
  17.             toast =Toast.makeText(context, s, Toast.LENGTH_SHORT);    
  18.             toast.show();    
  19.             oneTime=System.currentTimeMillis();    
  20.         }else{  
  21.             twoTime=System.currentTimeMillis();    
  22.             if(s.equals(oldMsg)){    
  23.                 if(twoTime-oneTime>Toast.LENGTH_SHORT){    
  24.                     toast.show();    
  25.                 }  
  26.             }else{    
  27.                 oldMsg = s;    
  28.                 toast.setText(s);    
  29.                 toast.show();    
  30.             }  
  31.         }  
  32.         oneTime=twoTime;    
  33.     }  
  34. }  


6、获取sdcard根目录

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. * 获取SD卡的状态  
  2. public static String getState(){  
  3.     return Environment.getExternalStorageState();  
  4. }  
  5.   
  6. /** 
  7.  * SD卡是否可用 
  8.  * @return 只有当SD卡已经安装并且准备好了才返回true 
  9.  */  
  10. public static boolean isAvailable(){  
  11.     return getState().equals(Environment.MEDIA_MOUNTED);  
  12. }  
  13.   
  14. /** 
  15.  * 获取SD卡的根目录 
  16.  * @return null:不存在SD卡 
  17.  */  
  18. public static File getRootDirectory(){  
  19.     return isAvailable()?Environment.getExternalStorageDirectory():null;  
  20. }  


 7、版本检测的实体类

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. public class VersionInfo implements Serializable{  
  2.     /** 
  3.      *  
  4.      */  
  5.     private static final long serialVersionUID = 1L;  
  6.     private String id;  
  7.     private String  versionName;//版本名  
  8.     private int     versionCode;//版本号  
  9.     private String  versionDesc;//版本描述信息内容  
  10.     private String  downloadUrl;//新版本的下载路径  
  11.     private String versionSize;//版本大小  
  12.       
  13.       
  14.     public String getId() {  
  15.         return id;  
  16.     }  
  17.     public void setId(String id) {  
  18.         this.id = id;  
  19.     }  
  20.     public String getVersionSize() {  
  21.         return versionSize;  
  22.     }  
  23.     public void setVersionSize(String versionSize) {  
  24.         this.versionSize = versionSize;  
  25.     }  
  26.     public String getVersionName() {  
  27.         return versionName;  
  28.     }  
  29.     public void setVersionName(String versionName) {  
  30.         this.versionName = versionName;  
  31.     }  
  32.     public int getVersionCode() {  
  33.         return versionCode;  
  34.     }  
  35.     public void setVersionCode(int versionCode) {  
  36.         this.versionCode = versionCode;  
  37.     }  
  38.     public String getVersionDesc() {  
  39.         return versionDesc;  
  40.     }  
  41.     public void setVersionDesc(String versionDesc) {  
  42.         this.versionDesc = versionDesc;  
  43.     }  
  44.     public String getDownloadUrl() {  
  45.         return downloadUrl;  
  46.     }  
  47.     public void setDownloadUrl(String downloadUrl) {  
  48.         this.downloadUrl = downloadUrl;  
  49.     }  
  50.       
  51. }  

8、网络监测
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <pre class="java" name="code">/** 
  2.  * 网络检查 
  3.  * @author 00 
  4.  * 
  5.  */  
  6. public class NetworkUtil {  
  7.     /** 
  8.      * 没有网络 
  9.      */  
  10.     public static final int NONETWORK = 0;  
  11.     /** 
  12.      * 当前是wifi连接 
  13.      */  
  14.     public static final int WIFI = 1;  
  15.     /** 
  16.      * 不是wifi连接 
  17.      */  
  18.     public static final int NOWIFI = 2;  
  19.       
  20.       
  21.     /** 
  22.      * 检测当前网络的类型 是否是wifi 
  23.      * @param context 
  24.      * @return 
  25.      */  
  26.     public static int checkedNetWorkType(Context context){  
  27.         if(!checkedNetWork(context)){  
  28.             return NONETWORK;  
  29.         }  
  30.         ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
  31.         if(cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting() ){  
  32.             return WIFI;  
  33.         }else{  
  34.             return NOWIFI;  
  35.         }  
  36.     }  
  37.       
  38.       
  39.     /** 
  40.      * 检查是否连接网络 
  41.      * @param context 
  42.      * @return 
  43.      */  
  44.     public static boolean  checkedNetWork(Context context){  
  45.         // 1.获得连接设备管理器  
  46.         ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);  
  47.         if(cm == nullreturn false;  
  48.         /** 
  49.          * 获取网络连接对象 
  50.          */  
  51.         NetworkInfo networkInfo = cm.getActiveNetworkInfo();  
  52.           
  53.         if(networkInfo == null || !networkInfo.isAvailable()){  
  54.             return false;  
  55.         }  
  56.         return true;  
  57.     }  
  58. }  

9、json解析类
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <pre class="java" name="code">/** 
  2.  * json 和 实体类之间的相互转换 
  3.  * @author 00 
  4.  * 
  5.  */  
  6. public class JsonUtil {  
  7.     /** 
  8.      * 将一个实体对象  转换成一个json字符串  提示对象中可包含集合 
  9.      * @param t 实体类 
  10.      * @return 
  11.      */  
  12.     public static <T> String beanToJson(T t){  
  13.         Gson gson = new Gson();  
  14.         String json = gson.toJson(t);  
  15.         return json;  
  16.     }  
  17.       
  18.     /** 
  19.      * 将一个json字符串 转换成一个实体类对象 可包含list 
  20.      * @param json 
  21.      * @param t 
  22.      * @return 
  23.      */  
  24.     public static <T> T jsonToBean(String json,Class<T> class1) throws InstantiationException, IllegalAccessException{  
  25.         Gson gson = new Gson();  
  26.         T t = class1.newInstance();  
  27.         t=gson.fromJson(json, class1);  
  28.         return t;  
  29.     }  
  30.       
  31.     /** 
  32.      * 将json字符串转换成一个json对象 
  33.      * @param str 
  34.      * @return 
  35.      */  
  36.     public static JSONObject stringToJson(String str){  
  37.         try {  
  38.             return new JSONObject(str);  
  39.         } catch (JSONException e) {  
  40.             e.printStackTrace();  
  41.             return null;  
  42.         }  
  43.     }  
  44.     public static String getString(InputStream is){  
  45.           
  46.         try {  
  47.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  48.               
  49.             byte[] buffer = new byte[1024];  
  50.             int len = -1;  
  51.             while((len = is.read(buffer)) != -1){  
  52.                 baos.write(buffer, 0, len);  
  53.             }  
  54.               
  55.             byte[] byteArray = baos.toByteArray();  
  56.             //String str = new String(byteArray);  
  57.               
  58.             return new String(byteArray,"utf-8");  
  59.         } catch (IOException e) {  
  60.             e.printStackTrace();  
  61.         }  
  62.           
  63.         return "";  
  64.     }  
  65.       
  66.     /** 
  67.      * 从assert文件夹中读取json文件,然后转化为json对象 
  68.      * @throws Exception  
  69.      */  
  70.     public static JSONObject getJsonDataFromAssets(Context context,String jsonFileName) throws Exception{  
  71.         JSONObject mJsonObj = null;  
  72.         StringBuffer sb = new StringBuffer();  
  73.         InputStream is = context.getAssets().open(jsonFileName);  
  74.         int len = -1;  
  75.         byte[] buf = new byte[1024];  
  76.         while ((len = is.read(buf)) != -1){  
  77.             sb.append(new String(buf, 0, len, "UTF-8"));  
  78.         }  
  79.         is.close();  
  80.         mJsonObj = new JSONObject(sb.toString());  
  81.         return mJsonObj;  
  82.     }  
  83.       
  84. }  

10、http请求类
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <pre class="java" name="code">/** 
  2.  * http  请求工具类 
  3.  * @author winfo-wj 
  4.  * 
  5.  */  
  6. public class HttpRequest {  
  7.       
  8.     private static HttpUtils http = new HttpUtils();  
  9.       
  10.       
  11.     /** 
  12.      * 请求回调接口 
  13.      * @author winfo-wj 
  14.      * 
  15.      */  
  16.     public interface RequestCallBackListener{  
  17.         /** 
  18.          * 请求成功  
  19.          * @param resultData    服务器返回的结果数据 
  20.          */  
  21.         public void onSuccess(String resultData);  
  22.           
  23.         /** 
  24.          * 请求失败 
  25.          * @param error 错误信息 
  26.          */  
  27.         public void onFailure(String error);  
  28.     }  
  29.       
  30.       
  31.       
  32.     /** 
  33.      * get请求  
  34.      * @param url 请求路径 
  35.      * @param requestCallBackListener 请求回调 
  36.      */  
  37.     public static void get(String url , final RequestCallBackListener requestCallBackListener){  
  38.         http.configTimeout(1000*10);  
  39.         http.send(HttpMethod.GET, url, new RequestCallBack<String>() {  
  40.   
  41.             @Override  
  42.             public void onSuccess(ResponseInfo<String> response) {  
  43.                 requestCallBackListener.onSuccess(response.result);  
  44.             }  
  45.               
  46.             @Override  
  47.             public void onFailure(HttpException error, String msg) {  
  48.                 requestCallBackListener.onFailure(msg);  
  49.             }  
  50.         });  
  51.     }  
  52.       
  53.     /** 
  54.      * post请求 
  55.      * @param url   请求地址 
  56.      * @param params    请求参数 
  57.      * @param requestCallBackListener   请求回调 
  58.      */  
  59.     public static void post(String url ,RequestParams params , final RequestCallBackListener requestCallBackListener){  
  60.         http.configTimeout(1000*10);  
  61.         http.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {  
  62.   
  63.             @Override  
  64.             public void onSuccess(ResponseInfo<String> response) {  
  65.                 requestCallBackListener.onSuccess(response.result);  
  66.             }  
  67.               
  68.             @Override  
  69.             public void onFailure(HttpException error, String msg) {  
  70.                 requestCallBackListener.onFailure(msg);  
  71.             }  
  72.         });  
  73.     }  
  74. }  

12、通知栏的布局

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <pre class="html" name="code"><pre class="html" name="code"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:orientation="vertical"  
  4.     android:layout_height="fill_parent" >  
  5.   
  6.       
  7.     <LinearLayout   
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:orientation="horizontal"  
  11.         >  
  12.         <LinearLayout   
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:orientation="horizontal"  
  16.             android:layout_gravity="center_vertical"  
  17.             >  
  18.             <ImageView   
  19.                 android:layout_width="40dp"  
  20.                 android:layout_height="40dp"  
  21.                 android:layout_gravity="center"  
  22.                 android:src="@drawable/ic_launcher"  
  23.                 />  
  24.         </LinearLayout>  
  25.           
  26.         <LinearLayout   
  27.             android:layout_width="match_parent"  
  28.             android:layout_height="wrap_content"  
  29.             android:orientation="vertical"  
  30.             android:weightSum="3"  
  31.             android:paddingLeft="10dp"  
  32.             android:paddingRight="10dp"  
  33.             android:layout_gravity="center_vertical"  
  34.             >  
  35.             <TextView   
  36.                 android:id="@+id/msg"  
  37.                 android:text="正在下载:xxx"  
  38.                 android:layout_width="wrap_content"  
  39.                 android:layout_height="wrap_content"  
  40.                 android:textColor="#ffffff"  
  41.                 android:textSize="14sp"  
  42.                 />  
  43.             <TextView   
  44.                 android:id="@+id/bartext"  
  45.                 android:layout_width="wrap_content"  
  46.                 android:layout_height="wrap_content"  
  47.                 android:text="0%"  
  48.                 android:textColor="#ffffff"  
  49.                 android:textSize="12sp"  
  50.                 />  
  51.             <ProgressBar  
  52.                 android:id="@+id/progressBar1"  
  53.                 style="?android:attr/progressBarStyleHorizontal"  
  54.                 android:layout_width="match_parent"  
  55.                 android:layout_height="5dp" />  
  56.               
  57.             <!--   
  58.             <LinearLayout   
  59.                 android:layout_width="match_parent"  
  60.                 android:layout_height="wrap_content"  
  61.                 android:orientation="horizontal"  
  62.                 >  
  63.                 <Button   
  64.                     android:id="@+id/btnStartStop"  
  65.                     android:layout_width="0dp"  
  66.                     android:layout_weight="1"  
  67.                     android:layout_height="25dp"  
  68.                     android:text="暂停"  
  69.                     android:textSize="12sp"  
  70.                     android:textColor="#ffffff"  
  71.                     />  
  72.                 <Button   
  73.                     android:id="@+id/btnCancel"  
  74.                     android:layout_width="0dp"  
  75.                     android:layout_weight="1"  
  76.                     android:layout_height="25dp"  
  77.                     android:text="取消"  
  78.                     android:textSize="12sp"  
  79.                     android:textColor="#ffffff"  
  80.                     />  
  81.             </LinearLayout>  
  82.              -->  
  83.         </LinearLayout>  
  84.           
  85.     </LinearLayout>  
  86.       
  87. </LinearLayout>  

14、按本更新提示的对话框布局文件

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_marginLeft="60dp"  
  11.          android:layout_centerInParent="true"  
  12.         android:layout_marginRight="60dp"  
  13.         android:background="@drawable/dialog_bg"  
  14.         android:orientation="vertical" >  
  15.   
  16.         <!-- Title -->  
  17.   
  18.         <RelativeLayout  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="45dp" >  
  21.             <ImageView  
  22.                 android:id="@+id/umeng_wifi_indicator"  
  23.                 android:layout_width="30dp"  
  24.                 android:layout_height="30dp"  
  25.                 android:layout_centerVertical="true"  
  26.                 android:layout_marginLeft="10dp"  
  27.                 android:src="@drawable/ic_launcher" />  
  28.   
  29.             <TextView  
  30.                 android:layout_width="wrap_content"  
  31.                 android:layout_height="wrap_content"  
  32.                 android:layout_centerInParent="true"  
  33.                 android:text="发现新版本"  
  34.                 android:textSize="@dimen/normal_text_size"  
  35.                 android:textAppearance="?android:attr/textAppearanceLarge"  
  36.                 android:textColor="@color/black" />  
  37.               
  38.         </RelativeLayout>  
  39.   
  40.         <!-- split -->  
  41.   
  42.         <View  
  43.             android:layout_width="fill_parent"  
  44.             android:layout_height="0.5dp"  
  45.             android:layout_marginLeft="10dp"  
  46.             android:layout_marginRight="10dp"  
  47.             android:background="#d8d8d8" />  
  48.         <!-- Content -->  
  49.   
  50.         <ScrollView  
  51.             android:layout_width="fill_parent"  
  52.             android:layout_height="0dp"  
  53.             android:padding="10dp"  
  54.             android:layout_weight="1" >  
  55.   
  56.             <LinearLayout  
  57.                 android:layout_width="fill_parent"  
  58.                 android:layout_height="wrap_content"  
  59.                 android:orientation="vertical" >  
  60.   
  61.                 <TextView   
  62.                     android:id="@+id/tv_update_title"  
  63.                     android:layout_width="match_parent"  
  64.                     android:layout_height="wrap_content"  
  65.                     android:text="最新版本:xxxv2.2.1"  
  66.                     android:textColor="@color/black"   
  67.                     android:textSize="@dimen/medium_text_size"  
  68.                     />  
  69.                   
  70.                 <TextView   
  71.                     android:id="@+id/tv_update_msg_size"  
  72.                     android:layout_width="match_parent"  
  73.                     android:layout_height="wrap_content"  
  74.                     android:text="新版本大小:19.07M"  
  75.                     android:textSize="@dimen/medium_text_size"  
  76.                     android:layout_marginTop="10dp"  
  77.                     android:textColor="@color/black"   
  78.                     />  
  79.                   
  80.                 <TextView  
  81.                     android:id="@+id/tv_update_content"  
  82.                     android:layout_width="fill_parent"  
  83.                     android:layout_height="wrap_content"  
  84.                     android:minHeight="60dp"  
  85.                     android:textSize="@dimen/medium_text_size"  
  86.                     android:lineSpacingExtra="3dp"  
  87.                     android:textColor="@color/black"   
  88.                     />  
  89.             </LinearLayout>  
  90.         </ScrollView>  
  91.           
  92.         <!-- Ignore CheckBox -->  
  93.   
  94.         <!-- OK&Cancel Button -->  
  95.   
  96.         <LinearLayout  
  97.             android:layout_width="fill_parent"  
  98.             android:orientation="horizontal"  
  99.             android:layout_height="wrap_content" >  
  100.   
  101.             <Button  
  102.                 android:id="@+id/btn_update_id_cancel"  
  103.                 android:layout_width="0dp"  
  104.                 android:layout_height="40dp"  
  105.                 android:layout_weight="1"  
  106.                 android:background="@drawable/dialog_cancel_btn_bg"  
  107.                 android:text="以后再说"  
  108.                 android:layout_marginLeft="10dp"  
  109.                 android:layout_marginRight="5dp"  
  110.                 android:layout_marginBottom="10dp"  
  111.                 android:textSize="@dimen/normal_text_size"  
  112.                 android:textColor="@color/black" />  
  113.               
  114.               
  115.             <Button  
  116.                 android:id="@+id/btn_update_id_ok"  
  117.                 android:layout_width="0dp"  
  118.                 android:layout_marginLeft="5dp"  
  119.                 android:layout_marginRight="10dp"  
  120.                 android:layout_marginBottom="10dp"  
  121.                 android:layout_height="40dp"  
  122.                 android:layout_weight="1"  
  123.                 android:textSize="@dimen/normal_text_size"  
  124.                 android:background="@drawable/dialog_ok_btn_bg"  
  125.                 android:text="立即更新"  
  126.                 android:textColor="@color/white" />  
  127.         </LinearLayout>  
  128.     </LinearLayout>  
  129.   
  130. </RelativeLayout>  


15、对话框按钮的.9图

 

16、字体颜色

<!-- 黑色 -->
    <color name="black">#333333</color>

 

17、字体大小文件

<dimen name="title_text_size">18sp</dimen><!-- 标题字体大小 -->
    <dimen name="normal_text_size">16sp</dimen><!-- 通常字体大小 -->
    <dimen name="medium_text_size">14sp</dimen><!-- 中等字体大小 -->
    <dimen name="small_text_size">12sp</dimen><!-- 小号字体大小 -->

0 0
原创粉丝点击