问题
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.
回答1:
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();
回答2:
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" />
回答3:
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"/>
回答4:
just remove Context
keyword in Context.TELEPHONY_SERVICE
and check
TelephonyManager tManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String IMEI = tManager.getDeviceId();
回答5:
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.
回答6:
[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.
来源:https://stackoverflow.com/questions/7514163/using-telephony-manager-in-android-to-find-imei-number