简体   繁体   English

在android中使用Telephony Manager查找IMEI号码

[英]Using Telephony Manager in android to find IMEI number

I m very new to Android Development. 我是Android开发的新手。 I want to find the IMEI number of the phone and using "android.telephony.TelephonyManager;". 我想找到手机的IMEI号并使用“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. 我缺少了什么步骤我还在XML中包含了用户权限。

Verify your Imports , you should import : android.content.Context , 验证你的Imports,你应该导入: 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 : 在您的Activity中,实例化您的类并将上下文传递给它,如下所示:

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

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" />

Try below piece of code it will help you to get device IMEI number. 尝试下面的代码,它将帮助您获取设备IMEI号码。

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"/>

just remove Context keyword in Context.TELEPHONY_SERVICE and check 只需在Context.TELEPHONY_SERVICE删除Context关键字并检查

TelephonyManager tManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);

String IMEI = tManager.getDeviceId();

For Compiler error "Context cannot be resolved to a variable", make sure you have imported android.content.Context package. 对于编译器错误“上下文无法解析为变量”,请确保已导入android.content.Context包。
In Eclipse, quick fixes would have it when you move mouse pointer over the error line in code. 在Eclipse中,当您将鼠标指针移动到代码中的错误行上时,快速修复就会拥有它。
And make sure you have added READ_PHONE_STATE permission Manifiest file. 并确保您已添加READ_PHONE_STATE权限Manifiest文件。

[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. 由于运营商的各种约束,AFAIK,TelephonyManager.getLine1Number()不可靠。 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] 有一些基于java反射的黑客攻击,但因设备而异,因此这些黑客攻击有点无用[至少在支持的模型方面]

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. 这个技巧的额外好处:1。如果设备中有多个SIM卡,您可以获得所有行号。

Cons: 1. you will need SMS_READ permission [sorry for that] 2. You will get all the sim numbers ever used in the device. 缺点:1。您将需要SMS_READ权限[对不起] 2.您将获得设备中使用的所有SIM号码。 this problem can be minimized with some constraint logic eg time frame (sms received or sent only today) etc. It would be interesting to hear from others about how to improve this case. 这个问题可以通过一些约束逻辑来最小化,例如时间范围(仅在今天收到或发送的短信)等。听取其他人关于如何改进这种情况的信息会很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM