Using Telephony Manager in android to find IMEI number

I am very new to Android development. I want to find the IMEI number of the phone and use "android.telephony.TelephonyManager;".

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.getDeviceId(); 

Now the compiler says. Context cannot be allowed for a variable. Can anybody help me? What step am I missing? I also included user permission in XML.

+7
source share
6 answers

Check the import, 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 must pass the context to your new class in 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, create your class and pass the context to it as follows:

 YourClass instance = new YourClass(this); String IMEI = instance.getIMEINumber(); 
+14
source

Add to this code:

  TelephonyManager manager=(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); String deviceid=manager.getDeviceId(); //Device Id is IMEI number Log.d("msg", "Device id"+deviceid); 

Manifesto

  <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
+8
source

Try executing a piece of code to help you get the IMEI device 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 permission to read the status of the phone in your manifest.

 <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 
+4
source

just remove the Context keyword in Context.TELEPHONY_SERVICE and check

 TelephonyManager tManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); String IMEI = tManager.getDeviceId(); 
+2
source

For the compiler error โ€œContext could not be resolved by a variableโ€, make sure you import the android.content.Context package.
In Eclipse, quick fixes will occur when you move the mouse over the error line in the code.
Make sure you add the READ_PHONE_STATE Manifiest file.

+1
source

[If someone still stumbles, looking for this still existing century-old solution]

AFAIK, TelephonyManager.getLine1Number () is not reliable due to various operator restrictions. There are some Java-based hackers that vary from device to device, which makes these hacks useless [at least in terms of supported models]

But there is legitimate legitimate logic to find the number if you really need it. Request all sms SMS providers and get the number "To".

Additional advantages of this trick: 1. You can get all line numbers if there are several simulators on the device.

Cons: 1. you will need SMS_READ permission [sorry for that] 2. You will receive all SIM numbers ever used in the device. this problem can be minimized with some restriction logic, for example. time frames (SMS, received or sent only today), etc. It would be interesting to hear from others about how to improve this case.

0
source

All Articles