2013-01-23 62 views
1

我正在開發一個應用程序,其中我檢查電話的服務狀態,但是當電話處於飛行模式時它沒有響應,或者電話不具有SIM,那麼它也不工作。 plz幫助我的代碼是如何檢查服務中的電話狀態

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 

感謝低於事先

回答

0

以上不是正確的答案,因爲它只會告訴你,如果SIM卡不在服務中。

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

應該立即發送服務狀態並定期發送。如果您將sim狀態和服務狀態混合在一起,您將得到錯誤的結果 - 您將無法使用,但sim狀態仍將準備就緒。