Chandra Sekhar
Chandra Sekhar

Reputation: 19500

How can I get the deviceId of my Android emulator?

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

Answers (6)

jenos kon
jenos kon

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 " enter image description here

Upvotes: 0

Christopher Chalfant
Christopher Chalfant

Reputation: 601

Through the Android emulators:

  1. Click the "More" dots at the bottom of the side menu.
  2. Go to "Help" section at bottom of navigation panel on the left.
  3. Inside "Help" go to the "About" Tab.
  4. Here you will find the serial number. Which in some cases is called the "deviceId".

Upvotes: 2

Mehdi Khademloo
Mehdi Khademloo

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

Ashish Dwivedi
Ashish Dwivedi

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

Harsh Trivedi
Harsh Trivedi

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

Deepak
Deepak

Reputation: 1141

The command 'adb devices' also lists the active emulators, which can give the device id.

Upvotes: 5

Related Questions