[英]Using TelephonyManager in Android in a class that is NOT an activity
这个问题可能源于我仍然对“上下文”及其在Android中的使用感到困惑的事实,因为我还是Android的新手。 提前致谢。
我只是想使类具有获取电话的MDN或MEID的功能(我知道这不适用于平板电脑)。 我只在这条线上出现错误:
TelephonyManager tManager = (TelephonyManager) Test.getSystemService(Context.TELEPHONY_SERVICE);
Eclipse给我“上下文”错误。 我查看了这里的上下文:Android上的“上下文”是什么? 并发现第一个注释非常有帮助,但是它并没有告诉您不在活动类中时如何获取上下文。
有什么建议么?
完整代码如下:
package com.test.app;
import java.util.UUID;
import android.content.Context;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
public class Test {
public String getMDN_or_MEID() {
// getSystemService is a method from the Activity class. getDeviceID()
// will return the MDN or MEID of the device depending on which radio
// the phone uses (GSM or CDMA).
TelephonyManager tManager = (TelephonyManager) Test
.getSystemService(Context.TELEPHONY_SERVICE);
String uid = tManager.getDeviceId();
return uid;
}
}
将您的getMDN_or_MEID()
方法更改为getMDN_or_MEID(Context context)
并将一个Context
传递给它。
然后,您所需要做的就是使用context.getSystemService(Context.TELEPHONY_MANAGER)
来获取TelephonyManager
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.