Android获取系统唯一识别码

| No Comments | No TrackBacks

特殊应用需要白名单机器才能访问

官方链接: Android Identifying App Installations 

需要在Manifest中申请android.permission.READ_PHONE_STATE

直接贴代码: 

public class helper {

private static String TAG="util.helper";

private static String SYS_ID = null;

public static String getUniqDeviceId(Context context) {
String id = getUniqueID(context);
if (id == null)
id = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
return id;
}



public static String getStringIntegerHexBlocks(int value) {
String result = "";
String string = Integer.toHexString(value);

int remain = 8 - string.length();
char[] chars = new char[remain];
Arrays.fill(chars, '0');
string = new String(chars) + string;

int count = 0;
for (int i = string.length() - 1; i >= 0; i--) {
count++;
result = string.substring(i, i + 1) + result;
if (count == 4) {
result = "-" + result;
count = 0;
}
}

if (result.startsWith("-")) {
result = result.substring(1, result.length());
}

return result;
}

private static String getUniqueID(Context context) {

if ( SYS_ID != null ) {
return SYS_ID;
}

String telephonyDeviceId;
String androidDeviceId;

String error = "0000-0000-0000-0000";

// get telephony id
try {
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyDeviceId = tm.getDeviceId();
if (telephonyDeviceId == null) {
Log.e(TAG, "Get TelephonyManager DeviceId(IMEI) Error");
return error;
}
Log.e(TAG, "TelephonyManager getDeviceId: " + telephonyDeviceId);
} catch (Exception e) {
Log.e(TAG, "Get TelephonyManager DeviceId(IMEI) Error" + e.getMessage());
return error;
}

// get internal android device id
try {
androidDeviceId = android.provider.Settings.Secure.getString(context.getContentResolver(),
android.provider.Settings.Secure.ANDROID_ID);
if (androidDeviceId == null) {
Log.e(TAG, "Get androidDeviceId Error");
return error;
}
Log.e(TAG, "android.provider.Settings.Secure.ANDROID_ID : " + androidDeviceId );
} catch (Exception e) {
Log.e(TAG, "Get androidDeviceId Error"+e.getMessage());
return error;
}

// build up the uuid
try {
String id = getStringIntegerHexBlocks(androidDeviceId.hashCode())
+ "-"
+ getStringIntegerHexBlocks(telephonyDeviceId.hashCode());

SYS_ID = id;
Log.e(TAG, SYS_ID);
return SYS_ID;
} catch (Exception e) {
return error;
}
}
}

No TrackBacks

TrackBack URL: http://blog.suchasplus.com/mt/mt-tb.cgi/346

Leave a comment

About this Entry

This page contains a single entry by suchasplus published on February 28, 2015 10:45 PM.

几个矩阵运算库 was the previous entry in this blog.

clink-gow is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 5.2.7