0

I need to access the IMEI number of android device. I'm not going to publish this android app on playstore. It's for personal use in the organization. Is there any workaround to fetch the IMEI number? Right now I'm geting security exception while fetching IMEI. I have made device admin app. Though I'm not able to access IMEI number.

class MainActivity : AppCompatActivity() {
    val REQUEST_CODE = 101
    var imei: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // in the below line, we are initializing our variables.

        // in the below line, we are initializing our variables.
        val telephonyManager = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
        val imeiTextView = findViewById<TextView>(R.id.idTVIMEI)

        // in the below line, we are checking for permissions

        // in the below line, we are checking for permissions
        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_PHONE_STATE
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            // if permissions are not provided we are requesting for permissions.
            ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.READ_PHONE_STATE),
                REQUEST_CODE
            )
        }

        // in the below line, we are setting our imei to our text view.

        // in the below line, we are setting our imei to our text view.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            imei = telephonyManager.imei
        }
        imeiTextView.setText(imei)
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == REQUEST_CODE) {
            // in the below line, we are checking if permission is granted.
            if (grantResults.size != 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // if permissions are granted we are displaying below toast message.
                Toast.makeText(this, "Permission granted.", Toast.LENGTH_SHORT).show()
            } else {
                // in the below line, we are displaying toast message
                // if permissions are not granted.
                Toast.makeText(this, "Permission denied.", Toast.LENGTH_SHORT).show()
            }
        }
    }
}
5
  • 1
    You need the READ_PRIVILEGED_PHONE_STATE permission. Which requires your app to be privlidged- basically you need to root the device and install it differently. Commented Nov 16, 2022 at 6:30
  • I have already added that permission. I want to get the IMEI of an unrooted device. Is there any way for getting the IMEI of the unrooted device? Commented Nov 17, 2022 at 5:39
  • You need to be a privlidged app or be the default dialer. Or since its a personal app, you can try reducing your targetSDKVersion to below 29, which is when restrictions went into place. That may work, at least for a few more Android versions. Commented Nov 17, 2022 at 7:02
  • It's throwing an error if I set target SDK less than 30 in the android studio. Is there any way to make the app privileged? Commented Nov 17, 2022 at 8:04
  • You need root to make the app privlidged. It's about where its installed to. The error on targetSDK can likely be ignored or turned off- you can't upload an app like that to the Play Store, but as long as you download the proper version of the SDK in AS you should be able to build for it Commented Nov 17, 2022 at 14:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.