1

我正在开发一个应用程序,我在其中检查手机的服务状态,但是当手机处于飞行模式时它没有响应,或者手机没有 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 

提前致谢

4

2 回答 2

1

检查手机是否处于飞行模式以及是否存在 SIM 卡,请参阅此链接

飞行模式链接: 如何在 Android 上检测飞行模式?

SIM 卡链接: 如何检查 SIM 卡在安卓设备中是否可用?

于 2013-01-23T13:59:18.560 回答
0

以上不是正确的答案,因为它仅告诉您 SIM 卡是否在使用中。

TelephonyManager.listen(PhoneStateListener() { ... }, PhoneStateListener.LISTEN_SERVICE_STATE);

应立即并定期发出服务状态。如果混合使用 sim state 和 service state,你会得到错误的结果——你会停止服务,但 sim state 仍然是准备好的。

于 2014-03-07T14:51:22.223 回答