APP设备相关

1. 获取app版本号

 // 取得版本号
   public static String GetVersion(Context context) {
   try {
      PackageInfo manager = context.getPackageManager().getPackageInfo(
            context.getPackageName(), 0);
      return manager.versionName;
   } catch (NameNotFoundException e) {
      return "Unknown";
   }
}

2.获取APPkey

// 取得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;
}

3.获取IMEI(IMEI是手机硬件的一个串码,类似我们的身份证,是手机硬件的唯一标识,手机在出厂的时候已经灌定在手机内核里了。很多软件都会绑定IMEI

public static String getImei(Context context, String imei) {
       String ret = null;
   try {
      TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
           ret = telephonyManager.getDeviceId();
   } catch (Exception e) {
      Logger.e(ExampleUtil.class.getSimpleName(), e.getMessage());
   }
   if (isReadableASCII(ret)){
           return ret;
       } else {
           return imei;
       }
}

   private static boolean isReadableASCII(CharSequence string){
       if (TextUtils.isEmpty(string)) return false;
       try {
           Pattern p = Pattern.compile("[\\x20-\\x7E]+");
           return p.matcher(string).matches();
       } catch (Throwable e){
           return true;
       }
   }

整理自极光Demo


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页