类型getSystemService(String)
BluetoothDevice
未定义
在我的手机上访问我的SIM卡的CLASS TelephonyInfo
正在运行
我想用它来显示我连接的设备上的SIM卡详细信息,我做了一些更改以包含蓝牙,但我遇到了methodgetSystemService
的问题
方法getSystemService(String)
未定义类型BluetoothDevice
,
我发现了一些类似的问题,但没有人使用蓝牙,我尝试了他们的解决方案,但由于我使用蓝牙,它没有用。这是我做了一些更改之后的代码
我正在使用这个类来显示我所连接的蓝牙设备的Sim细节,我有班级TelephonyInfo
public final class TelephonyInfo {
private static TelephonyInfo telephonyInfo;
private String imeiSIM1;
private String imeiSIM2;
private boolean isSIM1Ready;
private boolean isSIM2Ready;
public String getImeiSIM1() {
return imeiSIM1;
}
/*public static void setImeiSIM1(String imeiSIM1) {
TelephonyInfo.imeiSIM1 = imeiSIM1;
}*/
public String getImeiSIM2() {
return imeiSIM2;
}
/*public static void setImeiSIM2(String imeiSIM2) {
TelephonyInfo.imeiSIM2 = imeiSIM2;
}*/
public boolean isSIM1Ready() {
return isSIM1Ready;
}
/*public static void setSIM1Ready(boolean isSIM1Ready) {
TelephonyInfo.isSIM1Ready = isSIM1Ready;
}*/
public boolean isSIM2Ready() {
return isSIM2Ready;
}
/*public static void setSIM2Ready(boolean isSIM2Ready) {
TelephonyInfo.isSIM2Ready = isSIM2Ready;
}*/
public boolean isDualSIM() {
return imeiSIM2 != null;
}
private TelephonyInfo() {
}
public static TelephonyInfo getInstance(BluetoothDevice bluetoothDevice){
if(telephonyInfo == null) {
telephonyInfo = new TelephonyInfo();
TelephonyManager telephonyManager = ((TelephonyManager)
bluetoothDevice.getSystemService(Context.TELEPHONY_SERVICE));
telephonyInfo.imeiSIM1 = telephonyManager.getDeviceId();;
telephonyInfo.imeiSIM2 = null;
try {
telephonyInfo.imeiSIM1 = getDeviceIdBySlot(bluetoothDevice,
"getDeviceIdGemini", 0);
telephonyInfo.imeiSIM2 = getDeviceIdBySlot(bluetoothDevice,
"getDeviceIdGemini", 1);
} catch (GeminiMethodNotFoundException e) {
e.printStackTrace();
try {
telephonyInfo.imeiSIM1 = getDeviceIdBySlot(bluetoothDevice,
"getDeviceId", 0);
telephonyInfo.imeiSIM2 = getDeviceIdBySlot(bluetoothDevice,
"getDeviceId", 1);
} catch (GeminiMethodNotFoundException e1) {
//Call here for next manufacturer's predicted method name if you
wish
e1.printStackTrace();
}
}
telephonyInfo.isSIM1Ready = telephonyManager.getSimState() ==
TelephonyManager.SIM_STATE_READY;
telephonyInfo.isSIM2Ready = false;
try {
telephonyInfo.isSIM1Ready = getSIMStateBySlot(bluetoothDevice,
"getSimStateGemini", 0);
telephonyInfo.isSIM2Ready = getSIMStateBySlot(bluetoothDevice,
"getSimStateGemini", 1);
} catch (GeminiMethodNotFoundException e) {
e.printStackTrace();
try {
telephonyInfo.isSIM1Ready = getSIMStateBySlot(bluetoothDevice,
"getSimState", 0);
telephonyInfo.isSIM2Ready = getSIMStateBySlot(bluetoothDevice,
"getSimState", 1);
} catch (GeminiMethodNotFoundException e1) {
//Call here for next manufacturer's predicted method name if you
wish
e1.printStackTrace();
}
}
}
return telephonyInfo;
}
private static String getDeviceIdBySlot(BluetoothDevice bluetoothDevice,
String predictedMethodName, int slotID) throws
GeminiMethodNotFoundException {
String imei = null;
TelephonyManager telephony = (TelephonyManager)
bluetoothDevice.getSystemService(Context.TELEPHONY_SERVICE);
try{
Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
Class<?>[] parameter = new Class[1];
parameter[0] = int.class;
Method getSimID = telephonyClass.getMethod(predictedMethodName,
parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
Object ob_phone = getSimID.invoke(telephony, obParameter);
if(ob_phone != null){
imei = ob_phone.toString();
}
} catch (Exception e) {
e.printStackTrace();
throw new GeminiMethodNotFoundException(predictedMethodName);
}
return imei;
}
private static boolean getSIMStateBySlot(BluetoothDevice bluetoothDevice,
String predictedMethodName, int slotID) throws
GeminiMethodNotFoundException {
boolean isReady = false;
TelephonyManager telephony = (TelephonyManager)
bluetoothDevice.getSystemService(Context.TELEPHONY_SERVICE);
try{
Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
Class<?>[] parameter = new Class[1];
parameter[0] = int.class;
Method getSimStateGemini = telephonyClass.getMethod(predictedMethodName,
parameter);
Object[] obParameter = new Object[1];
obParameter[0] = slotID;
Object ob_phone = getSimStateGemini.invoke(telephony, obParameter);
if(ob_phone != null){
int simState = Integer.parseInt(ob_phone.toString());
if(simState == TelephonyManager.SIM_STATE_READY){
isReady = true;
}
}
} catch (Exception e) {
e.printStackTrace();
throw new GeminiMethodNotFoundException(predictedMethodName);
}
return isReady;
}
private static class GeminiMethodNotFoundException extends Exception {
private static final long serialVersionUID = -996812356902545308L;
public GeminiMethodNotFoundException(String info) {
super(info);
}
}
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
getSystemService()是上下文类的方法,因此您需要在上下文中运行它。
您复制它的原始代码可能是从 Activity 派生的类中运行的。如果它不在活动中,您需要将上下文参数传递给您的方法。