原文地址:http://www.2cto.com/kf/201208/151990.html

 

我们这些苦逼的程序员在工作中,我们的每一个老板都希望我们都能把手头的工作做好的,而且是越快越好,那我们要怎么样才起来呢?对于常用的代码块无限复做是我们工作中简省时间最有效的途径之一,而下面的这些代码就是我们在开发出现概率较多的,就为大家归纳了一部分开发中常用的代码块:


一 隐藏软键盘的输入法

 

 
  
  1. InputMethodManager mInputMethodManager = (InputMethodManager) context 
  2. .getSystemService(Context.INPUT_METHOD_SERVICE); 
  3. mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);  

二 判断网络是否是好的  

 

 
  
  1. public static boolean isActiveNetwork(Context context) { 
  2.     ConnectivityManager cManager = (ConnectivityManager) context 
  3.     .getSystemService(Context.CONNECTIVITY_SERVICE); 
  4.     NetworkInfo netInfor = cManager.getActiveNetworkInfo(); 
  5.     if (netInfor != null && netInfor.isAvailable()) { 
  6.         return true; 
  7.     } else { 
  8.         return false; 
  9.     }  

三 数据单位的转换

 

 
  
  1. /** 
  2.  * 转化B到KB 
  3.  */ 
  4.  public static double transB2KB(long b) { 
  5.     return b / 1024; 
  6.  
  7. /** 
  8.  * 转化B到KB 
  9.  */ 
  10. public static double transKB2M(double KB) { 
  11.     return KB / 1024; 

四  确保文件目录存在


 

 
  
  1. public static void checkFileDirectory(String path) { 
  2.     if (path != null) { 
  3.         File filePath = new File(path); 
  4.         if (!filePath.exists()) { 
  5.             filePath.mkdirs(); 
  6.         } 
  7.     } 

五 获取网络文件的总大小


 

 
  
  1. public static Long getTotalSize(String url) { 
  2.     Long totalSize = null
  3.     try { 
  4.         totalSize = NetworkUtil.getContentSize(url); 
  5.     } catch (Exception e) { 
  6.         totalSize = 0L
  7.         e.printStackTrace(); 
  8.     } 
  9.     return totalSize; 

六 显示网络异常的提示

 

 
  
  1. public static void showNetException(Context context) { 
  2.     Toast.makeText(context, 
  3.             context.getApplicationContext().getResources().getString(R.string.net_exception), 
  4.             Toast.LENGTH_SHORT).show(); 

七 java将天数转换为毫秒数


 

 
  
  1. public static long transDayToTime(long datCount) { 
  2.     long time = datCount * 24 * 60 * 60 * 1000; 
  3.     return time; 

八 java 将毫秒数转换为天数


 

 
  
  1. public static int transTimeToDay(long time) { 
  2.     int day = (int) (time / (24 * 60 * 60 * 1000)); 
  3.     return day; 

九 android判断应用是否是内置的


 

 
  
  1. public static boolean isSystemApplication(Context context, String packageName) { 
  2.     boolean isflag = false
  3.     try { 
  4.         PackageManager pm = context.getPackageManager(); 
  5.         ApplicationInfo pInfo = pm 
  6.         .getApplicationInfo(packageName, PackageManager.GET_META_DATA); 
  7.         if ((pInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { 
  8.             isflag = true
  9.         } 
  10.     } catch (Exception e) { 
  11.         Log.i("xxxxx","Exception "); 
  12.     } 
  13.     return isflag; 
 
  


十 判断字符串是否为空

 

 
  
  1. public static boolean isNull(String string) { 
  2.     if (string != null) { 
  3.         stringstring = string.trim(); 
  4.         if (string.length() != 0) { 
  5.             return false; 
  6.         } 
  7.     } 
  8.     return true;