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()
}
}
}
}