Using Telephony Manager in android to find IMEI number
Asked Answered
P

6

14

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.

Pindling answered 22/9, 2011 at 11:41 Comment(0)
Q
22

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();
Querulous answered 22/9, 2011 at 11:42 Comment(10)
its okey now but next time first read question carefully after then give answer in proper wayPadauk
i've read the question carefully , i've read it before it has been edited, and the question was : get the IMEI Number and that is not more explicated :)Querulous
he was asked question with sample code which is right now in question but that was not in good format so i just added his question.. but he asked question with sample code.. and one more thing brother I dont want to fight with you I am just suggesting you nothing else..Padauk
lol , me too , i'm not fighting you , come ooon !!! i'm just telling you too that i've read the qustion carrefuly , and thanks for ur suggestions :)Querulous
Thank u all , i didn't imported android.content.Context.Pindling
but now says getSystemService... The method getSystemService(String) is undefined for the typePindling
do you call your code at the method onCreate() of your Activity ? if yes , so try to replace the getSystemService() with this.getSystemService(String)Querulous
its working fine on onCreate method of activity. but what i m doing is that this function is called on an another class which i have createdPindling
in this case , you should pass the context to your new class , see my editQuerulous
This is a Context leak, only do it if you know the lifecycle of "YourClass"Immediate
D
9

Add in this code:

 TelephonyManager manager=(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
 String deviceid=manager.getDeviceId();

//Device Id is IMEI number

  Log.d("msg", "Device id"+deviceid);

Manifest

   <uses-permission android:name="android.permission.READ_PHONE_STATE" />
Duckboard answered 26/6, 2012 at 7:41 Comment(0)
P
4

Try below piece of code it will help you to get device IMEI number.

public String getDeviceID() { 
    String deviceId;   
    TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (mTelephony.getDeviceId() != null) {
            deviceId = mTelephony.getDeviceId(); 
        } else {
            deviceId = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID); 
        }
    return deviceId;
}

Also give the read phone state permission in your manifest.

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Phasia answered 2/11, 2015 at 5:23 Comment(0)
P
2

just remove Context keyword in Context.TELEPHONY_SERVICE and check

TelephonyManager tManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);

String IMEI = tManager.getDeviceId();
Padauk answered 22/9, 2011 at 11:55 Comment(0)
O
1

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.

Oeillade answered 22/9, 2011 at 11:55 Comment(0)
F
0

[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.

Frond answered 7/1, 2015 at 12:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.