如何检查android中的手机服务状态

时间:2013-01-16 09:41:55

标签: android

我正在开发一个应用程序,我正在尝试检查手机的服务状态

使用服务状态:IN_SERVICE 如果手机是IN_SERVICE生成Toast手机正在服务,如果它没有生成Toast手机没有服务  我正在尝试这个代码

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);  
    }
}

无法获得服务状态:IN_SERVICE请帮助我 提前致谢

1 个答案:

答案 0 :(得分:5)

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);