Here’s the content of my MainActivity class where I log the IMEI of my test device.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package net.catzie.playground; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.telephony.TelephonyManager; import android.util.Log; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); String deviceID = telephonyManager.getDeviceId(); Log.d("Device IMEI", deviceID); } } |
You also need to add this permission to your AndroidManifest.xml file:
1 |
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> |
Place it inside the <manifest></manifest> tag, but same level as <application></application>.
Source: How to get the device’s IMEI/ESN programmatically in android?