To identify each devices uniquely I would like to use the IMEI (or ESN number for CDMA devices). How to access this programmatically?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
For Android 6.0+ the game has changed so i suggest you use this;
The best way to go is during runtime else you get permission errors.
Hope this helps you or someone.
I use the following code to get the IMEI or use Secure.ANDROID_ID as an alternative, when the device doesn't have phone capabilities:
In addition to the answer of Trevor Johns, you can use this as follows:
And you should add the following permission into your Manifest.xml file:
In emulator, you'll probably get a like a "00000..." value. getDeviceId() returns NULL if device ID is not available.
Kotlin Code for getting DeviceId ( IMEI ) with handling permission & comparability check for all android versions :
Also add this permission to AndroidManifest.xml :
For those looking for a Kotlin version, you can use something like this;
NOTE: You must wrap
telephonyService()
above with a permission check using checkSelfPermission or whatever method you use.Also add this permission in the manifest file;
You want to call
android.telephony.TelephonyManager.getDeviceId()
.This will return whatever string uniquely identifies the device (IMEI on GSM, MEID for CDMA).
You'll need the following permission in your
AndroidManifest.xml
:<uses-permission android:name="android.permission.READ_PHONE_STATE" />
in order to do this.
That being said, be careful about doing this. Not only will users wonder why your application is accessing their telephony stack, it might be difficult to migrate data over if the user gets a new device.
Update: As mentioned in the comments below, this is not a secure way to authenticate users, and raises privacy concerns. It is not recommended. Instead, look at the Google+ Login API if you want to implement a frictionless login system.
The Android Backup API is also available if you just want a lightweight way to persist a bundle of strings for when a user resets their phone (or buys a new device).