android:获取或创建每个设备的唯一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) –