我是 Android 开发的新手。 我想找到手机的 IMEI 号码并使用“android.telephony.TelephonyManager;”。
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getDeviceId();
现在编译器说。 上下文无法解析为变量。 任何人都可以帮助我吗?我缺少哪一步 我还在 XML 中包含了用户权限。
最佳答案
验证您的导入,您应该导入:android.content.Context
,
然后使用这段代码:
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// get IMEI
String imei = tm.getDeviceId();
//get The Phone Number
String phone = tm.getLine1Number();
或者直接:使用这个:
TelephonyManager tm = (TelephonyManager) getSystemService(android.content.Context.TELEPHONY_SERVICE);
编辑: *您应该将上下文传递给构造函数上的新类:*
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
}
}
然后在您的 Activity 中,实例化您的类并将上下文传递给它,如下所示:
YourClass instance = new YourClass(this);
String IMEI = instance.getIMEINumber();
关于android - 在 android 中使用 Telephony Manager 查找 IMEI 号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7514163/