Ashfaque
Ashfaque

Reputation: 1284

How to check phone in service State

I am developing an app in which i m checking service state of the phone but when phone is in flight mode its not responding, or phone dont have sim then also its not working. plz help me code is below

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 

thanks in advance

Upvotes: 1

Views: 2291

Answers (2)

CompEng88
CompEng88

Reputation: 1532

The above isn't the correct answer since it only tells you if the sim card is in not if it is in service.

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

should emmit the service state instantly AND periodically. You'll get the wrong results if you mix sim state and service state -- you'll be out of service, but sim state will still be ready.

Upvotes: 0

DevfaR
DevfaR

Reputation: 1616

check whether the phone is on Airplane mode and if the sim card is present see this link

Airplane mode link: How can one detect airplane mode on Android?

SIM card link: How can I check whether the Sim Card is available in an android device?

Upvotes: 1

Related Questions