1421fc72be462e402014c5f51007efbf69a1c89a.svn-base 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package eVVM.apk.jpush;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.content.pm.ApplicationInfo;
  5. import android.content.pm.PackageInfo;
  6. import android.content.pm.PackageManager;
  7. import android.content.pm.PackageManager.NameNotFoundException;
  8. import android.net.ConnectivityManager;
  9. import android.net.NetworkInfo;
  10. import android.os.Bundle;
  11. import android.os.Looper;
  12. import android.telephony.TelephonyManager;
  13. import android.text.TextUtils;
  14. import android.widget.Toast;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17. import cn.jpush.android.api.JPushInterface;
  18. import cn.jpush.android.helper.Logger;
  19. public class ExampleUtil {
  20. public static final String PREFS_NAME = "JPUSH_EXAMPLE";
  21. public static final String PREFS_DAYS = "JPUSH_EXAMPLE_DAYS";
  22. public static final String PREFS_START_TIME = "PREFS_START_TIME";
  23. public static final String PREFS_END_TIME = "PREFS_END_TIME";
  24. public static final String KEY_APP_KEY = "JPUSH_APPKEY";
  25. public static boolean isEmpty(String s) {
  26. if (null == s)
  27. return true;
  28. if (s.length() == 0)
  29. return true;
  30. if (s.trim().length() == 0)
  31. return true;
  32. return false;
  33. }
  34. /**
  35. * 只能以 “+” 或者 数字开头;后面的内容只能包含 “-” 和 数字。
  36. */
  37. private final static String MOBILE_NUMBER_CHARS = "^[+0-9][-0-9]{1,}$";
  38. public static boolean isValidMobileNumber(String s) {
  39. if (TextUtils.isEmpty(s)) return true;
  40. Pattern p = Pattern.compile(MOBILE_NUMBER_CHARS);
  41. Matcher m = p.matcher(s);
  42. return m.matches();
  43. }
  44. // 校验Tag Alias 只能是数字,英文字母和中文
  45. public static boolean isValidTagAndAlias(String s) {
  46. Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_!@#$&*+=.|]+$");
  47. Matcher m = p.matcher(s);
  48. return m.matches();
  49. }
  50. // 取得AppKey
  51. public static String getAppKey(Context context) {
  52. Bundle metaData = null;
  53. String appKey = null;
  54. try {
  55. ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
  56. context.getPackageName(), PackageManager.GET_META_DATA);
  57. if (null != ai)
  58. metaData = ai.metaData;
  59. if (null != metaData) {
  60. appKey = metaData.getString(KEY_APP_KEY);
  61. if ((null == appKey) || appKey.length() != 24) {
  62. appKey = null;
  63. }
  64. }
  65. } catch (NameNotFoundException e) {
  66. }
  67. return appKey;
  68. }
  69. // 取得版本号
  70. public static String GetVersion(Context context) {
  71. try {
  72. PackageInfo manager = context.getPackageManager().getPackageInfo(
  73. context.getPackageName(), 0);
  74. return manager.versionName;
  75. } catch (NameNotFoundException e) {
  76. return "Unknown";
  77. }
  78. }
  79. public static void showToast(final String toast, final Context context) {
  80. new Thread(new Runnable() {
  81. @Override
  82. public void run() {
  83. Looper.prepare();
  84. Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
  85. Looper.loop();
  86. }
  87. }).start();
  88. }
  89. public static boolean isConnected(Context context) {
  90. ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  91. NetworkInfo info = conn.getActiveNetworkInfo();
  92. return (info != null && info.isConnected());
  93. }
  94. @SuppressLint("MissingPermission")
  95. public static String getImei(Context context, String imei) {
  96. String ret = null;
  97. try {
  98. TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  99. ret = telephonyManager.getDeviceId();
  100. } catch (Exception e) {
  101. Logger.e(ExampleUtil.class.getSimpleName(), e.getMessage());
  102. }
  103. if (isReadableASCII(ret)) {
  104. return ret;
  105. } else {
  106. return imei;
  107. }
  108. }
  109. private static boolean isReadableASCII(CharSequence string) {
  110. if (TextUtils.isEmpty(string)) return false;
  111. try {
  112. Pattern p = Pattern.compile("[\\x20-\\x7E]+");
  113. return p.matcher(string).matches();
  114. } catch (Throwable e) {
  115. return true;
  116. }
  117. }
  118. public static String getDeviceId(Context context) {
  119. return JPushInterface.getUdid(context);
  120. }
  121. }