Android监听SIM卡插拔的方式

监听SIM卡是否可用

import android.app.Service;  
import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;  
import android.telephony.TelephonyManager;  

/* 
 * 监听sim状态改变的广播,返回sim卡的状态, 有效或者无效。 
 * 双卡中只要有一张卡的状态有效即返回状态为有效,两张卡都无效则返回无效。 
 */ 
public class SimStateReceive extends BroadcastReceiver {  
    private final static String ACTION_SIM_STATE_CHANGED = "android.intent.action.SIM_STATE_CHANGED";  
    private final static int SIM_VALID = 0;  
    private final static int SIM_INVALID = 1;  
    private int simState = SIM_INVALID;  
      
    public int getSimState() {  
        return simState;  
    }  
  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        System.out.println("sim state changed");  
        if (intent.getAction().equals(ACTION_SIM_STATE_CHANGED)) {  
            TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);   
            int state = tm.getSimState();  
            switch (state) {  
            case TelephonyManager.SIM_STATE_READY :  
                simState = SIM_VALID;  
                break;  
            case TelephonyManager.SIM_STATE_UNKNOWN :  
            case TelephonyManager.SIM_STATE_ABSENT :  
            case TelephonyManager.SIM_STATE_PIN_REQUIRED :  
            case TelephonyManager.SIM_STATE_PUK_REQUIRED :  
            case TelephonyManager.SIM_STATE_NETWORK_LOCKED :  
            default:  
                simState = SIM_INVALID;  
                break;  
            }  
        }  
    }  
  
}  

双SIM卡,监听状态

SIM卡状态改变的action是android.intent.action.SIM_STATE_CHANGED,需要在Manifest.xml进行注册。

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    final String action = intent.getAction();
    Log.i(TAG,"++++++Action:"+action);
    if("android.intent.action.SIM_STATE_CHANGED".equals(action)){
        Log.d(TAG,"###############start");
        setSimInfoByChange(context,intent);
    }
}
private void setSimInfoByChange(Context context,Intent intent){
    //subid 卡ID
    int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
    //soltId 卡槽ID 0:卡槽一  1:卡槽二
    int soltId = intent.getIntExtra("slot",SOLTID);
    Log.d(TAG, " #######subId:"+subId);
    Bundle mBundle = intent.getExtras();//从中可以看到intent的发送过来的数据

    Log.d(TAG, " #######mBundle:"+mBundle.toString());
    String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
    if (stateExtra!=null) {
        if (stateExtra.equals("ABSENT")) { //卡拔出状态
            SIM_STATE = 1;
            SimInfo = context.getString(R.string.cell_broadcast_widget_no_sim_card, "No SIM Cards");
        } else if (stateExtra.equals("READY") ||  //卡正常状态  即可以读出卡信息
                stateExtra.equals("IMSI") ||
                stateExtra.equals("LOADED") ){
            SIM_STATE = 0;
        }else if(stateExtra.equals("LOCKED") || //卡被锁状态 
                stateExtra.equals("NOT_READY") ||
                stateExtra.equals("PIN")||
                stateExtra.equals("PUK")){
            SIM_STATE = 2;
            SimInfo = context.getString(R.string.cell_broadcast_widget_no_service,"No Services");
        }
    }
    Log.d(TAG, " #######stateExtra:"+stateExtra+"+++++SimInfo:"+SimInfo);
    Log.d(TAG, " #######soltId:"+soltId);
}

通过这个intent可以找到相应的SIM及卡槽信息,同时

String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);

这个方法可以获取SIM卡改变的状态情况,在icccardconstants的API中可以发现有这些状态:

com.android.Internal.telephony.IccCard

type name value
protected static final int EVENT_ICC_LOCKED_OR_ABSENT 1
protected static final int EVENT_ICC_READY 6
protected static final int EVENT_RADIO_OFF_OR_NOT_AVALIABLE 3
public static final java.lang.String INTENT_KEY_ICC_STATE "ss"
public static final java.lang.String INTENT_KEY_LOCKED_REASON "reason"
public static final java.lang.String INTENT_VALUE_ABSENT_ON_PERM_DIASBLED "PERM_DISABLED"
public static final java.lang.String INTENT_VALUE_ICC_ABSENT "ABSENT"
public static final java.lang.String INTENT_VALUE_ICC_IMSI "IMSI"
public static final java.lang.String INTENT_VALUE_ICC_LOADED "LOADED"
public static final java.lang.String INTENT_VALUE_ICC_LOCKED "LOCKED"
public static final java.lang.String INTENT_VALUE_ICC_NOT_READY "NOT_READY"
public static final java.lang.String INTENT_VALUE_ICC_READY "READY"
public static final java.lang.String INTENT_VALUE_LOCKED_NETWORK "NETWORK"
public static final java.lang.String INTENT_VALUE_LOCKED_ON_PIN "PIN"
public static final java.lang.String INTENT_VALUE_LOCKED_ON_PUK "PUK"

你可能感兴趣的:(Android监听SIM卡插拔的方式)