1. 程式人生 > >工具方法:判斷是否為空獲取手機Imei編號等

工具方法:判斷是否為空獲取手機Imei編號等

 public static boolean isEmpty(String s) {
       if (null == s)
           return true;
       if (s.length() == 0)
           return true;
       if (s.trim().length() == 0)
           return true;
       return false;
   }
   
   // 校驗Tag Alias 只能是數字,英文字母和中文
public static boolean isValidTagAndAlias(String s) {
       Pattern p = Pattern.compile
("^[\u4E00-\u9FA5[email protected]#$&*+=.|]+$"); Matcher m = p.matcher(s); return m.matches(); } // 取得AppKey public static String getAppKey(Context context) { Bundle metaData = null; String appKey = null; try { ApplicationInfo ai = context.getPackageManager().getApplicationInfo( context.getPackageName(), PackageManager.GET_META_DATA
); if (null != ai) metaData = ai.metaData; if (null != metaData) { appKey = metaData.getString(KEY_APP_KEY); if ((null == appKey) || appKey.length() != 24) { appKey = null; } } } catch
(NameNotFoundException e) { } return appKey; } // 取得版本號 public static String GetVersion(Context context) { try { PackageInfo manager = context.getPackageManager().getPackageInfo( context.getPackageName(), 0); return manager.versionName; } catch (NameNotFoundException e) { return "Unknown"; } } public static String getDeviceId(Context context) { String deviceId = JPushInterface.getUdid(context); return deviceId; } public static void showToast(final String toast, final Context context) { new Thread(new Runnable() { @Override public void run() { Looper.prepare(); Toast.makeText(context, toast, Toast.LENGTH_SHORT).show(); Looper.loop(); } }).start(); } public static boolean isConnected(Context context) { ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = conn.getActiveNetworkInfo(); return (info != null && info.isConnected()); } public static String getImei(Context context, String imei) { try { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); imei = telephonyManager.getDeviceId(); } catch (Exception e) { Log.e(ExampleUtil.class.getSimpleName(), e.getMessage()); } return imei; }