我正在开发一个应用程序,其中我检查手机的服务状态,但当手机处于飞行模式时,它没有响应,或者手机没有SIM卡,那么它也无法正常工作。 PLZ帮我代码在下面
TelephonyManager telMng=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telMng.listen(new PhoneStateListener(){
@Override
public void onServiceStateChanged (ServiceState serviceState){
super.onServiceStateChanged(serviceState);
String phonestate;
switch(serviceState.getState())
{
case ServiceState.STATE_IN_SERVICE:
Toast.makeText(getApplicationContext(), "phone is in service", Toast.LENGTH_LONG).show();
break;
case ServiceState.STATE_OUT_OF_SERVICE:
Toast.makeText(getApplicationContext(), "Phone is not in Service state", Toast.LENGTH_LONG).show();
break;
default:phonestate="Unknown";
}
/*
switch(serviceState.getState()){
case ServiceState.STATE_EMERGENCY_ONLY: phonestate ="STATE_EMERGENCY_ONLY"; ;break;
case ServiceState.STATE_IN_SERVICE: phonestate ="STATE_IN_SERVICE"; ;break;
case ServiceState.STATE_OUT_OF_SERVICE: phonestate ="STATE_OUT_OF_SERVICE"; ;break;
case ServiceState.STATE_POWER_OFF: phonestate ="STATE_POWER_OFF"; ;break;
default:phonestate = "Unknown";
}
*/
}
},
PhoneStateListener.LISTEN_SERVICE_STATE);
}
i tried above code but its not working when phone is in flight mode or have not sim insert inside the phone what should i do plz help
提前致谢
答案 0 :(得分:1)
检查手机是否处于飞行模式,如果存在SIM卡,请参阅此链接
飞机模式链接: How can one detect airplane mode on Android?
SIM卡链接: How can I check whether the Sim Card is available in an android device?
答案 1 :(得分:0)
以上不是正确答案,因为它只会告诉您SIM卡是否在服务中。
TelephonyManager.listen(PhoneStateListener(){...},PhoneStateListener.LISTEN_SERVICE_STATE);
应该立即和周期性地发出服务状态。如果你混合使用sim状态和服务状态,你会得到错误的结果 - 你将停止服务,但是sim状态仍然准备就绪。