验证你的进口,你应该导入: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
}
}
而在你的活动,实例化类,并通过上下文中这样说:
YourClass instance = new YourClass(this);
String IMEI = instance.getIMEINumber();
是一样的他的问题.. –
看到我的编辑... – Houcine
它的好,但现在下一次先读问题后,仔细然后给答案正确的方式 –