我正在開發一個應用程序使用API 14(android 4.0)。android:獲取或創建每個設備的唯一ID
在清單:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
我想從得到一個唯一的ID每個設備(或創建一個),可能是即使重啓設備後相同。但重要的是即使對於2個相同的設備,ID也是不同的。我怎樣才能做到這一點?
我正在開發一個應用程序使用API 14(android 4.0)。android:獲取或創建每個設備的唯一ID
在清單:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
我想從得到一個唯一的ID每個設備(或創建一個),可能是即使重啓設備後相同。但重要的是即使對於2個相同的設備,ID也是不同的。我怎樣才能做到這一點?
您可以使用GCM生成不同的設備令牌.... 即使您將卸載並重新安裝應用程序或出廠設置後,此設備令牌仍將保持不變。您必須執行一些步驟.... .. 在Google Developers Console上創建一個新項目。 在這一步,爲了簡單起見,您只需要注意2個值:項目編號,它將在客戶端項目中用作SENDER_ID;和API服務器密鑰(在Credentials中創建),它將在服務器項目中用作API_KEY。 爲服務器端創建一個新的簡單的Android項目(基本源代碼作爲我在下面的鏈接中的答案)。
爲客戶端創建一個新的簡單Android項目(基本源代碼作爲我在下面的鏈接中的答案,我從Google雲消息傳遞的原始源代碼定製 - GitHub)。
運行客戶端應用程序,您將獲得註冊令牌(意味着您的設備已成功註冊)。然後,將該令牌粘貼(硬編碼)到服務器應用程序中的CLIENT_REGISTRATION_TOKEN變量(或編寫代碼以將此令牌發送到服務器應用程序)。 您可以在下面的問題更多,其中一人已與你以前的一個問題之前閱讀:
如何使用Android Studio來實現GCM的Hello World的Android 添加谷歌雲Messagin(GCM)爲Android - 註冊過程
您可以使用設備的IMEI號碼作爲唯一的ID。
您想致電android.telephony.TelephonyManager.getDeviceId()。
這將返回任何字符串唯一標識設備(GSM上的IMEI,CDMA的MEID)。
你需要以下權限在AndroidManifest.xml中:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
試試這個String android_id = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
這裏android_id是每個設備的唯一的ID。
試試這個代碼
String UniqueDeviceId = AndroidDeviceIdentifier.getUniqueDeviceIdentifier(context);
添加這個類了。
final class AndroidDeviceIdentifier {
private AndroidDeviceIdentifier() {
// hidden constructor of singleton
}
/**
* Returns a stable identifier for the current device.
*
* @param ctx The application's Context
* @return The unique device identifier
* @throws IllegalStateException If the device's identifier could not be determined
*/
public static String getUniqueDeviceIdentifier(@NonNull final Context ctx) throws IllegalStateException {
try {
return getDeviceUUID(ctx);
} catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
throw new IllegalStateException("Could not determine device identifier", e);
}
}
private static String getDeviceUUID(Context ctx) throws UnsupportedEncodingException, NoSuchAlgorithmException {
byte[] hash = makeHash(getMac(ctx), getSerialNumber(ctx));
return createUUIDFromHash(hash);
}
private static String createUUIDFromHash(byte[] hash) {
return UUID.nameUUIDFromBytes(hash).toString().toLowerCase(); // Server side wants lower cased UUIDs
}
private static byte[] makeHash(final String mac, final String serialNumber) throws UnsupportedEncodingException, NoSuchAlgorithmException {
MessageDigest sha;
sha = MessageDigest.getInstance("SHA-256");
sha.reset();
sha.update(mac.getBytes("UTF-8"));
sha.update(serialNumber.getBytes("UTF-8"));
return sha.digest();
}
private static String getSerialNumber(Context context) {
String serialNumber = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
if (serialNumber == null) {
serialNumber = "0000000000000000";
}
return serialNumber;
}
private static String getMac(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
String mac = wifiManager.getConnectionInfo().getMacAddress();
if (mac == null) {
mac = "000000000000";
}
return mac;
}
您將得到一個唯一的設備ID。如果您有任何問題,請給我打電話
如果我的回答滿足你的需求,請標記爲正確答案 –
您是否嘗試過任何操作? –
我已經使用Google搜索,但是沒有發現適用於所有設備(如手機,平板電腦和其他設備)的任何內容。 –
[是否有唯一的Android設備ID?](http:// stackoverflow。com/questions/2785485/is-there-a-unique-android-device-id) –