我正在开发一个应用程序,我在其中检查手机的服务状态,但是当手机处于飞行模式时它没有响应,或者手机没有 SIM 卡,那么它也无法工作。请帮我代码如下
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
提前致谢