Using Telephony Manager in android to find IMEI nu

2019-02-02 16:56发布

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.

标签: android imei
6条回答
贼婆χ
2楼-- · 2019-02-02 17:34

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.

查看更多
姐就是有狂的资本
3楼-- · 2019-02-02 17:44

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();
查看更多
祖国的老花朵
4楼-- · 2019-02-02 17:46

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" />
查看更多
孤傲高冷的网名
5楼-- · 2019-02-02 17:46

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

查看更多
Rolldiameter
6楼-- · 2019-02-02 17:52

just remove Context keyword in Context.TELEPHONY_SERVICE and check

TelephonyManager tManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);

String IMEI = tManager.getDeviceId();
查看更多
Explosion°爆炸
7楼-- · 2019-02-02 17:58

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"/>
查看更多
登录 后发表回答