2

電話のサービス状態を確認しようとしているアプリを開発しています

サービス状態の使用: IN_SERVICE 電話が IN_SERVICE の場合は Toast phone is in service を生成します そうでない場合は Toast phone is not in service を生成します このコードを試しています

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 に答える