• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

Android

android获取建筑唯一标识完善计划

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/Android/2019/0920/651.html

 
 
 
/**</span></p>  * deviceID的组成为:渠道标志+辨认符来源标志+hash后的终端辨认符
 
  *
 
  * 常州软件技术培训渠道标志为:
 
  * 1,andriod(a)
 
  *
 
  * 辨认符来源标志:
 
  * 1, wifi mac地点(wifi);
 
  * 2, IMEI(imei);
 
  * 3, 序列号(sn);
 
  * 4, id:随机码。若前面的都取不到时,则随机生成一个随机码,需求缓存。
 
  *
 
  * @param context
 
  * @return
 
  */
 
public static String getDeviceId(Context context) {
 
  StringBuilder deviceId = new StringBuilder();
 
  // 渠道标志
 
  deviceId.append("a");
 
  try {
 
    //wifi mac地点
 
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
 
    WifiInfo info = wifi.getConnectionInfo();
 
    String wifiMac = info.getMacAddress();
 
    if(!isEmpty(wifiMac)){
 
      deviceId.append("wifi");
 
      deviceId.append(wifiMac);
 
      PALog.e("getDeviceId : ", deviceId.toString());
 
      return deviceId.toString();
 
    }
 
    //IMEI(imei)
 
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
 
    String imei = tm.getDeviceId();
 
    if(!isEmpty(imei)){
 
      deviceId.append("imei");
 
      deviceId.append(imei);
 
      PALog.e("getDeviceId : ", deviceId.toString());
 
      return deviceId.toString();
 
    }
 
    //序列号(sn)
 
    String sn = tm.getSimSerialNumber();
 
    if(!isEmpty(sn)){
 
      deviceId.append("sn");
 
      deviceId.append(sn);
 
      PALog.e("getDeviceId : ", deviceId.toString());
 
      return deviceId.toString();
 
    }
 
    //要是上头都没有, 则生成一个id:随机码
 
    String uuid = getUUID(context);
 
    if(!isEmpty(uuid)){
 
      deviceId.append("id");
 
      deviceId.append(uuid);
 
      PALog.e("getDeviceId : ", deviceId.toString());
 
      return deviceId.toString();
 
    }
 
  } catch (Exception e) {
 
    e.printStackTrace();
 
    deviceId.append("id").append(getUUID(context));
 
  }
 
  PALog.e("getDeviceId : ", deviceId.toString());
 
  return deviceId.toString();
 
}
 
/**
 
 * 获得常州平台运营全局唯一UUID
 
 */
 
public static String getUUID(Context context){
 
  SharedPreferences mShare = getSysShare(context, "sysCacheMap");
 
  if(mShare != null){
 
    uuid = mShare.getString("uuid", "");
 
  }
 
  if(isEmpty(uuid)){
 
    uuid = UUID.randomUUID().toString();
 
    saveSysMap(context, "sysCacheMap", "uuid", uuid);
 
  }
 
  PALog.e(tag, "getUUID : " + uuid);
 
return uuid;
 
}
 
偶然需求对用户建筑进行标识,所以有望能够获得一个稳定靠得住而且唯一的辨认码。固然Android体系中提供了如许建筑辨认码,不过因为Android体系版本、厂约定制体系中的Bug等限制,稳定性和唯一性并不睬想。而通过其余硬件信息标识也因为体系版本、手机硬件等限制存在差别程度的题目。
 
 
 
下面网络了少许“有才气”或“有必然才气”作为建筑标识的串码。
 
 
 
DEVICE_ID
 
这是Android体系为开发者提供的用于标识手机建筑的串号,也是种种方法中普适性较高的,可以说险些所有的建筑都可以回笼这个串号,而且唯一性良好。
 
 
 
这个DEVICE_ID可以同通过下面的方法获取:
 
 
 
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
 
String DEVICE_ID = tm.getDeviceId(); 
 
假定我们确实需求用到实在建筑的标识,可能就需求用到DEVICE_ID。在过去,我们的Android建筑是手机,这个DEVICE_ID可以同通过TelephonyManager.getDeviceId()获取,它凭据差别的手机建筑回笼IMEI,MEID大概ESN码,但它在应用的过程中会遇到许多题目:
 
 
 
非手机建筑: 要是只带有Wifi的建筑大概音乐播放器没有通话的硬件功效的话就没有这个DEVICE_ID
 
权限: 获取DEVICE_ID需求READ_PHONE_STATE权限,但要是我们只为了获取它,没有效到其余的通话功效,那这个权限有点大才小用
 
bug:在小批的少许手机建筑上,该实现有漏洞,会回笼废品,如:zeros大概asterisks的产品
 
MAC ADDRESS
 
可以应用手机Wifi或蓝牙的MAC地点作为建筑标识,不过并不推荐这么做,缘故有以下两点:
 
 
 
硬件限制:并不是所有的建筑都有Wifi和蓝牙硬件,硬件不存在天然也就得不到这一信息。
 
获取的限制:要是Wifi没有翻开过,是无法获取其Mac地点的;而蓝牙是惟有在翻开的时候才气获取到其Mac地点。
 
获取Wifi Mac地点:
 
 
 
获取蓝牙 Mac地点:
 
 
 
Sim Serial Number
 
装有SIM卡的Android 2.3建筑,可以通过下面的方法获取到Sim Serial Number:
 
 
 
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
 
String SimSerialNumber = tm.getSimSerialNumber(); 
 
留意:对于CDMA建筑,回笼的是一个空值!
 
 
 
ANDROID_ID
 
在建筑初次启动时,体系会随机生成一个64位的数字,并把这个数字以16进制字符串的形式留存下来,这个16进制的字符串即是ANDROID_ID,当建筑被wipe后该值会被重置。可以通过下面的方法获取:
 
 
 
import android.provider.Settings;
 
String ANDROID_ID = Settings.System.getString(getContentResolver(), Settings.System.ANDROID_ID);
 
ANDROID_ID可以作为建筑标识,但需求留意:
 
厂约定制体系的Bug:差别的建筑可能会发生相像的ANDROID_ID:9774d56d682e549c。
 
厂约定制体系的Bug:有些建筑回笼的值为null。
 
建筑差异:对于CDMA建筑,ANDROID_ID和TelephonyManager.getDeviceId() 回笼常州微信公众平台相像的值。
 
它在Android <=2.1 or Android >=2.3的版本是靠得住、稳定的,但在2.2的版本并不是100%靠得住的
 
Serial Number
 
Android体系2.3版本以上可以通过下面的方法获得Serial Number,且非手机建筑也能够通过该接口获取。
 
 
 
String SerialNumber = android.os.Build.SERIAL; 
 
以上几种方法都或多或少存在必然的局限性大概Bug,要是并不是确实需求对硬件本身进行绑定,应用本人生成的UUID也是一个不错的选定,因为该方法无需走访建筑的资源,也跟建筑范例无关。
 
 
 
Installtion ID
 
这种方法的道理是在程序安置后第一次运转时生成一个ID,该方法和建筑唯一标识差别样,差别的运用程序会发生差别的ID,统一个程序从新安置也会差别。所以这不是建筑的唯一ID,不过可以包管每个用户的ID是差别的。可以说是用来标识每一份运用程序的唯一ID(即Installtion ID),可以用来跟踪运用的安置数目等。
 
 
 
Google Developer Blog提供了如许的一个框架:
 
 
 
public class Installation {
 
    private static String sID = null;
 
    private static final String INSTALLATION = "INSTALLATION";
 
 
 
    public synchronized static String id(Context context) {
 
        if (sID == null) {  
 
            File installation = new File(context.getFilesDir(), INSTALLATION);
 
            try {
 
                if (!installation.exists())
 
                    writeInstallationFile(installation);
 
                sID = readInstallationFile(installation);
 
            } catch (Exception e) {
 
                throw new RuntimeException(e);
 
            }
 
        }
 
        return sID;
 
    }
 
 
 
    private static String readInstallationFile(File installation) throws IOException {
 
        RandomAccessFile f = new RandomAccessFile(installation, "r");
 
        byte[] bytes = new byte[(int) f.length()];
 
        f.readFully(bytes);
 
        f.close();
 
        return new String(bytes);
 
    }
 
 
 
    private static void writeInstallationFile(File installation) throws IOException {
 
        FileOutputStream out = new FileOutputStream(installation);
 
        String id = UUID.randomUUID().toString();
 
        out.write(id.getBytes());
 
        out.close();
 
    }
 
}
 
建筑唯一ID
 
上文可以看出,Android体系中并无可以靠得住获取所有厂商建筑唯一ID的方法,各个方法都有本人的应用局限和局限性,这也是当前盛行的Android体系版本过量,建筑也是来自差别厂商,且没有统一尺度等缘故造成的。
 
 
 
从当前发展来看,Android体系多版本共存还会连接较长的时间,而Android体系也不会被某个建筑制造厂商垄断,久远看Android底子体系将会趋于稳定,建筑标识也将会作为体系底子片面而尺度化,届时这一题目才有望完全办理。
 
 
 
当前的办理办法,对照可行的是逐一适配,在包管大无数建筑利便的条件下,要是获取不到,应用其余备选信息作为标识,即本人再封装一个建筑ID出来,通过里面算法包管尽管和建筑硬件信息关联,以及标识的唯一性。
 
 
 
总结
 
 
 
概括以上所述,为了实现在建筑上更通用的获取建筑唯一标识,我们可以实现如许的一个类,为每个建筑发生唯一的UUID,以ANDROID_ID为底子,在获取失利时以TelephonyManager.getDeviceId()为备选方法,要是再失利,应用UUID的生成策略。
 
 
 
重申下,以下方法是生成Device ID,在大无数环境下Installtion ID能够满足我们的需求,不过要是确实需求用到Device ID,那可以通过以下方法实现:
 
 
 
import android.content.Context;
 
import android.content.SharedPreferences;
 
import android.provider.Settings.Secure;
 
import android.telephony.TelephonyManager;
 
import java.io.UnsupportedEncodingException;
 
import java.util.UUID;
 
 
 
public class DeviceUuidFactory {
 
    protected static final String PREFS_FILE = "device_id.xml";
 
    protected static final String PREFS_DEVICE_ID = "device_id";
 
    protected static UUID uuid;
 
 
 
    public DeviceUuidFactory(Context context) {
 
        if( uuid ==null ) {
 
            synchronized (DeviceUuidFactory.class) {
 
                if( uuid == null) {
 
                    final SharedPreferences prefs = context.getSharedPreferences( PREFS_FILE, 0);
 
                    final String id = prefs.getString(PREFS_DEVICE_ID, null );
 
                    if (id != null) {
 
                        // Use the ids previously computed and stored in the prefs file
 
                        uuid = UUID.fromString(id);
 
                    } else {
 
                        final String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
 
                        // Use the Android ID unless it's broken, in which case fallback on deviceId,
 
                        // unless it's not available, then fallback on a random number which we store
 
                        // to a prefs file
 
                        try {
 
                            if (!"9774d56d682e549c".equals(androidId)) {
 
                                uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
 
                            } else {
 
                                final String deviceId = ((TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId();
 
                                uuid = deviceId!=null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID();
 
                            }
 
                        } catch (UnsupportedEncodingException e) {
 
                            throw new RuntimeException(e);
 
                        }
 
                        // Write the value out to the prefs file
 
                        prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString() ).co毫米it();
 
                    }
 
                }
 
            }
 
        }
 
    }
 
    /**
 
     * Returns a unique UUID for the current android device.  As with all UUIDs, this unique ID is "very highly likely"
 
     * to be unique across all Android devices.  Much more so than ANDROID_ID is.
 
     *
 
     * The UUID is generated by using ANDROID_ID as the base key if appropriate, falling back on
 
     * TelephonyManager.getDeviceID() if ANDROID_ID is known to be incorrect, and finally falling back
 
     * on a random UUID that's persisted to SharedPreferences if getDeviceID() does not return a
 
     * usable value.
 
     *
 
     * In some rare circumstances, this ID may change.  In particular, if the device is factory reset a new device ID
 
     * may be generated.  In addition, if a user upgrades their phone from certain buggy implementations of Android 2.2
 
     * to a newer, non-buggy version of Android, the device ID may change.  Or, if a user uninstalls your app on
 
     * a device that has neither a proper Android ID nor a Device ID, this ID may change on reinstallation.
 
     *
 
     * Note that if the code falls back on using TelephonyManager.getDeviceId(), the resulting ID will NOT
 
     * change after a factory reset.  Something to be aware of.
 
     *
 
     * Works around a bug in Android 2.2 for many devices when using ANDROID_ID directly.
 
     *
 
     * @see http://code.谷歌.com/p/android/issues/detail?id=10603
 
     *
 
     * @return a UUID that may be used to uniquely identify your device for most purposes.
 
     */
 
    public UUID getDeviceUuid() {
 
        return uuid;
 
    }
 
}
 
常州微信小程序开发若何获取Android手机的唯一标识?
 
代码: 这里是你在Android里读出 唯一的 IMSI-ID / IMEI-ID 的方法。
 
 
 
Java: 
 
String myIMSI = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMSI); 
 
// within my emulator it returns: 310995000000000 
 
 
 
String myIMEI = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMEI); 
 
// within my emulator it returns: 000000000000000 
 
注:android.os.SystemProperties的标签被打上@hide了,所以sdk中并不会存在。要是需求应用,需求有android的source code支持。
 

上篇:上一篇:Android6.0run时权限请求计划
下篇:下一篇:辅助功 AccessibilityService条记(3)