2

I tried using below piece of code but it is not giving me the number. Your information would be great help.

Code below:

val subscription =SubscriptionManager.from(context).activeSubscriptionInfoList
for (subscriptionInfo in subscription)
{
   val number = subscriptionInfo.number
   Log.e("Test", " Number is  " + number)
}
Community
  • 1
  • 1

3 Answers3

5

Correct way to get IMEI Number KOTLIN

try{
    val tm = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    val IMEI = tm.getImei()
    if (IMEI != null)
        Toast.makeText(this, "IMEI number: " + IMEI,
                Toast.LENGTH_LONG).show()

}catch (ex:Exception){
    Log.e("",ex.message)
}

Including asking for Permission

  if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.READ_PHONE_STATE)) {
    } else { ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.READ_PHONE_STATE), 2) } }

Don't forget AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Kingsley Mitchell
  • 2,412
  • 2
  • 18
  • 25
1

Taken from this answer and translated to kotlin:

Getting the Phone Number, IMEI, and SIM Card ID

val tm = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager

// For SIM card, use the getSimSerialNumber()    
//---get the SIM card ID---
val simID = tm.simSerialNumber
if (simID != null)
    Toast.makeText(this, "SIM card ID: " + simID,
            Toast.LENGTH_LONG).show()

Phone number of your phone, use the getLine1Number() (some device's dont return the phone number)

//---get the phone number---
val telNumber = tm.line1Number
if (telNumber != null)
    Toast.makeText(this, "Phone number: " + telNumber,
            Toast.LENGTH_LONG).show()


// IMEI number of the phone, use the getDeviceId()
//---get the IMEI number---
val IMEI = tm.deviceId
if (IMEI != null)
    Toast.makeText(this, "IMEI number: " + IMEI,
            Toast.LENGTH_LONG).show()

Permissions needed:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Please note that some devices could not return the phone number due to its internal implementation.

Kaaveh Mohamedi
  • 1,399
  • 1
  • 15
  • 36
crgarridos
  • 8,758
  • 3
  • 49
  • 61
  • I tried in 3 Samsung devices with android 5,6 and 7 versions. – Govind Rastogi Jan 12 '18 at 10:21
  • Yeah, Samsung is known for change the internal apis, anyway is difficult to achieve this in particular. take a look to [this answer](https://stackoverflow.com/a/41739519/1797950) also – crgarridos Jan 12 '18 at 10:26
0

Right now you can not access these Info according to docs:

  • getSerial()
  • getImei()
  • getDeviceId()
  • getMeid()
  • getSimSerialNumber()
  • getSubscriberId()

If your app targets Android 10 or higher, a SecurityException occurs.

If your app targets Android 9 (API level 28) or lower, the method returns null or placeholder data if the app has the READ_PHONE_STATE permission. Otherwise, a SecurityException occurs.

Kaaveh Mohamedi
  • 1,399
  • 1
  • 15
  • 36