【发布时间】:2012-02-04 21:18:11
【问题描述】:
我正在开发一个能够获得推送通知的安卓应用。但是我需要一个 deviceId 才能使其成功,并且由于我没有任何 android 手机,所以我曾经在模拟器中测试该应用程序。所以我的问题是,我可以为我的模拟器获取一个 deviceId。
【问题讨论】:
-
你的意思是ANDROID_ID吗? stackoverflow.com/questions/4402262/…
我正在开发一个能够获得推送通知的安卓应用。但是我需要一个 deviceId 才能使其成功,并且由于我没有任何 android 手机,所以我曾经在模拟器中测试该应用程序。所以我的问题是,我可以为我的模拟器获取一个 deviceId。
【问题讨论】:
获取模拟器的设备ID >>>
在 onCreate() 方法中添加这两行:
String android_id = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
String deviceId = md5(android_id).toUpperCase();
Log.i("device id=",deviceId);
在 onCreate() 方法之外添加这个md5() 方法:
public String md5(String s) {
try {
// Create MD5 Hash
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
for (int i=0; i<messageDigest.length; i++)
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
要查找设备 ID,只需在您的 Android Studio 中运行应用并打开 logcat,然后点击搜索栏中的“设备 ID”
【讨论】:
通过 Android 模拟器:
【讨论】:
这对我有用
public static String getIMEI() {
String IMEI = Settings.Secure.getString(getApplicationContext().getContentResolver(),Settings.Secure.ANDROID_ID);
return IMEI;
}
【讨论】:
使用此方法,此方法适用于平板电脑和手机两者
public String getDeviceID(Context context) {
TelephonyManager manager =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String deviceId;
if (manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE) {
//Tablet
deviceId = Secure.getString(this.getContentResolver(),
Secure.ANDROID_ID);
} else {
//Mobile
deviceId = manager.getDeviceId();
}
return deviceId;
}
【讨论】:
您无法在 android 中获取设备 ID,但您可以获取 IMEI 号码以进行推送通知。 bcoz 所有设备都有不同的 IMEI 号码。在模拟器中,默认情况下您会获得 0000000000000 作为您的 IMEI,但在设备中您会获得完美的数字。下面是获取IMEI号码的代码
TelephonyManager telephonyManager1 = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String imei = telephonyManager1.getDeviceId();
【讨论】:
“adb devices”命令还列出了活动的模拟器,可以提供设备 ID。
【讨论】: