Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Telephony Manager in android to find IMEI number

Tags:

android

imei

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.

like image 779
Arsalan Avatar asked Sep 22 '11 11:09

Arsalan


People also ask

How do I find my IMEI number on Android?

How to find the IMEI number on your Android phone: Open the Settings app on your Android phone. Scroll down and tap on About Phone. Scroll down and you will find the number under IMEI.

How do you use telephony manager?

The android. telephony. TelephonyManager class provides information about the telephony services such as subscriber id, sim serial number, phone network type etc. Moreover, you can determine the phone state etc.

Can Android apps read IMEI?

Android recognizes the importance of protecting the IMEI. In order for apps to access it, they must be granted the READ_PHONE_STATE permission (see figure above); only apps with that permission are allowed to access the IMEI.

Can Android 10 get programmatically IMEI number?

UPDATE: From Android 10 its is restricted for user apps to get non-resettable hardware identifiers like IMEI.


1 Answers

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();
like image 108
Houcine Avatar answered Oct 04 '22 18:10

Houcine