现在的位置: 首页 > 综合 > 正文

android监控SIM卡状态的广播示例代码

2016年10月16日 ⁄ 综合 ⁄ 共 1540字 ⁄ 字号 评论关闭
  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.   

抱歉!评论已关闭.