Reputation: 19500
I am developing an android app which can able to get push notifications. But I need to have a deviceId to make it successful and as I don't have any android phone, I used to test the app in emulator. So my question is, can I get a deviceId for my emulator.
Upvotes: 5
Views: 33425
Reputation: 564
To get the device Id of your emulator >>>
Inside onCreate() method add this two line :
String android_id = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
String deviceId = md5(android_id).toUpperCase();
Log.i("device id=",deviceId);
outside the onCreate() method add this md5() method :
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 "";
}
To find the device id just run the app and open the logcat in your android studio and tap in the search bar " device id "
Upvotes: 0
Reputation: 601
Through the Android emulators:
Upvotes: 2
Reputation: 2812
This works for me
public static String getIMEI() {
String IMEI = Settings.Secure.getString(getApplicationContext().getContentResolver(),Settings.Secure.ANDROID_ID);
return IMEI;
}
Upvotes: 0
Reputation: 8196
Use this method, this works for Tablet and Phone both
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;
}
Upvotes: 1
Reputation: 1010
you can't get device id in android but you can get IMEI number for push notification. bcoz all devices has different IMEI number. In Emulator you get by default 0000000000000 As your IMEI but in device you get perfect number. below is the code to get IMEI number
TelephonyManager telephonyManager1 = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String imei = telephonyManager1.getDeviceId();
Upvotes: 4
Reputation: 1141
The command 'adb devices' also lists the active emulators, which can give the device id.
Upvotes: 5