android SIM卡狀態

  1. /* 
  2.      監聽sim狀態改變的廣播,返回sim卡的狀態, 有效或者無效。 
  3.     雙卡中只要有一張卡的狀態有效即返回狀態爲有效,兩張卡都無效則返回無效。 
  4.  */  
  5. import android.app.Service;  
  6. import android.content.BroadcastReceiver;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.telephony.TelephonyManager;  
  10.   
  11. public class SimStateReceive extends BroadcastReceiver {  
  12.     private final static String ACTION_SIM_STATE_CHANGED = "android.intent.action.SIM_STATE_CHANGED";  
  13.     private final static int SIM_VALID = 0;  
  14.     private final static int SIM_INVALID = 1;  
  15.     private int simState = SIM_INVALID;  
  16.       
  17.     public int getSimState() {  
  18.         return simState;  
  19.     }  
  20.   
  21.     @Override  
  22.     public void onReceive(Context context, Intent intent) {  
  23.         System.out.println("sim state changed");  
  24.         if (intent.getAction().equals(ACTION_SIM_STATE_CHANGED)) {  
  25.             TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);   
  26.             int state = tm.getSimState();  
  27.             switch (state) {  
  28.             case TelephonyManager.SIM_STATE_READY :  
  29.                 simState = SIM_VALID;  
  30.                 break;  
  31.             case TelephonyManager.SIM_STATE_UNKNOWN :  
  32.             case TelephonyManager.SIM_STATE_ABSENT :  
  33.             case TelephonyManager.SIM_STATE_PIN_REQUIRED :  
  34.             case TelephonyManager.SIM_STATE_PUK_REQUIRED :  
  35.             case TelephonyManager.SIM_STATE_NETWORK_LOCKED :  
  36.             default:  
  37.                 simState = SIM_INVALID;  
  38.                 break;  
  39.             }  
  40.         }  
  41.     }  
  42.   
  43. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章