2

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

使用服务状态: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 请帮助我提前致谢

4

1 回答 1

6
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);
于 2013-01-16T10:02:21.260 回答