I m very new to Android Development. I want to find the IMEI number of the phone and using "android.telephony.TelephonyManager;".
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getDeviceId();
Now the compiler says. Context cannot be resolved to a variable. Any one can help me ? What step I m missing I have also included user permission in XML.
How to find the IMEI number on your Android phone: Open the Settings app on your Android phone. Scroll down and tap on About Phone. Scroll down and you will find the number under IMEI.
The android. telephony. TelephonyManager class provides information about the telephony services such as subscriber id, sim serial number, phone network type etc. Moreover, you can determine the phone state etc.
Android recognizes the importance of protecting the IMEI. In order for apps to access it, they must be granted the READ_PHONE_STATE permission (see figure above); only apps with that permission are allowed to access the IMEI.
UPDATE: From Android 10 its is restricted for user apps to get non-resettable hardware identifiers like IMEI.
Verify your Imports , you should import : android.content.Context
,
And then use this code :
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// get IMEI
String imei = tm.getDeviceId();
//get The Phone Number
String phone = tm.getLine1Number();
Or directly : use this :
TelephonyManager tm = (TelephonyManager) getSystemService(android.content.Context.TELEPHONY_SERVICE);
EDIT : *you should pass the context to your new Class on the constructor :*
public class YourClass {
private Context context;
//the constructor
public YourClass( Context _context){
this.context = _context;
//other initialisations .....
}
//here is your method to get the IMEI Number by using the Context that you passed to your class
public String getIMEINumber(){
//...... place your code here
}
}
And in your Activity , instanciate your class and pass the context to it like this :
YourClass instance = new YourClass(this);
String IMEI = instance.getIMEINumber();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With