这个问题可能源于我仍然对“上下文”及其在 Android 中的用法感到困惑这一事实,因为我对 Android 还是很陌生。提前致谢。
我只是想创建一个具有获取手机 MDN 或 MEID 功能的类(我知道这在平板电脑上不起作用)。我只在这一行收到错误:
TelephonyManager tManager = (TelephonyManager) Test.getSystemService(Context.TELEPHONY_SERVICE);
Eclipse 在“上下文”上给我一个错误。我查看了这里的上下文: What is 'Context' on Android? 并发现第一条评论非常有帮助,但它没有告诉您如何在不在 Activity 类(class)中时获取上下文。
有什么建议吗?
完整代码如下:
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
...
关于java - 在不是 Activity 的类中使用 Android 中的 TelephonyManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9832856/