Ashfaque
Ashfaque

Reputation: 1284

How to check phone service state in android

I am developing an app in which i am trying to check the service state of phone

using Service state: IN_SERVICE if the phone is IN_SERVICE generate a Toast phone is in service if its not then generate a Toast phone is not in service im trying this code

public class MyphoneStateListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
     switch (state) {  
        case TelephonyManager.CALL_STATE_IDLE:   
            break;  
        case TelephonyManager.CALL_STATE_RINGING: 
           sendemail(); 
            break;  
        case TelephonyManager.CALL_STATE_OFFHOOK:  
        default:  
            break;  
        }  
        super.onCallStateChanged(state, incomingNumber);  
    }
}

not able to get the Service state: IN_SERVICE Please help me Thanks in advance

Upvotes: 2

Views: 7970

Answers (1)

VinceFR
VinceFR

Reputation: 2571

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_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";
                break;
        }   
    } 
}, PhoneStateListener.LISTEN_SERVICE_STATE);

Upvotes: 6

Related Questions