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.
For Compiler error "Context cannot be resolved to a variable", make sure you have imported
android.content.Context
package.In Eclipse, quick fixes would have it when you move mouse pointer over the error line in code.
And make sure you have added
READ_PHONE_STATE
permission Manifiest file.Verify your Imports , you should import :
android.content.Context
,And then use this code :
Or directly : use this :
EDIT : *you should pass the context to your new Class on the constructor :*
And in your Activity , instanciate your class and pass the context to it like this :
Add in this code:
Manifest
[In case if anyone still stumbles here looking for this still existing age-old solution]
AFAIK, TelephonyManager.getLine1Number() is not reliable due to various constraints from operators. There are some java reflection based hacks but varies from device to device thus making those hacks sort of useless [at least in terms of supported models]
But there is a legitimate lawful logic to find the number, if you really need so. Query all the SMS by sms provider and get the "To" number.
Extra benefits of this trick: 1. you can get all the line numbers if there is multi sim in the device.
Cons: 1. you will need SMS_READ permission [sorry for that] 2. You will get all the sim numbers ever used in the device. this problem can be minimized with some constraint logic e.g. time frame (sms received or sent only today) etc. It would be interesting to hear from others about how to improve this case.
just remove
Context
keyword inContext.TELEPHONY_SERVICE
and checkTry below piece of code it will help you to get device IMEI number.
Also give the read phone state permission in your manifest.