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.   

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值