如何监控SIM卡状态变化如何监控SIM卡状态变化(How to monitor SIM state

2019-05-14 00:06发布

我希望能够做一些东西时,需要SIM卡的PIN当SIM状态变化,即播放声音,但我认为是可以通过这个广播接收机截获没有广播事件......对于注册android.intent.action .PHONE_STATE不只能告诉你,当呼叫状态的变化..另一种可以开始是在一个LISTEN_SERVICE_STATE注册一个PhoneStateListener和反应的服务(状态是当OUT-OF-STATE它可以得到的SIM从TelephonyManager状态看,如果状态SIM_STATE_PIN_REQUIRED)。 所以,我的问题是:

1)是否有,我可以用它来拦截SIM状态改变或服务状态变化的任何广播意图是什么?

2)它是一个坏主意,在服务中安装一个PhoneStateListener,并用它在手机状态的通知,交付意图服务本身改由PhoneStateListener收到?

Answer 1:

这样做的目的android.intent.action.SIM_STATE_CHANGED在广播SIM状态变化时。 例如,在我与T-Mobile的SIM卡的HTC Desire,如果我把设备进入飞行模式下意图广播:

  • 意图:android.intent.action.SIM_STATE_CHANGED与演员:SS = NOT_READY,原因= NULL

如果我再取出来的飞行模式,下面的意图是广播:

  • 意图:android.intent.action.SIM_STATE_CHANGED与演员:SS =锁定,原因= PIN
  • 意图:与演员android.intent.action.SIM_STATE_CHANGED:SS = READY,原因= NULL
  • 意图:与演员android.intent.action.SIM_STATE_CHANGED:SS = IMSI,原因= NULL
  • 意图:与演员android.intent.action.SIM_STATE_CHANGED:SS =受压,原因= NULL

这可能是不同厂家和不同型号不同的表现。 正如他们所说,“您的情况可能会有所不同。”



Answer 2:

大卫的答案是当场上。 我想添加一些示例代码,以帮助人们开始实现这样的状态监视器。

/**
 * Handles broadcasts related to SIM card state changes.
 * <p>
 * Possible states that are received here are:
 * <p>
 * Documented:
 * ABSENT
 * NETWORK_LOCKED
 * PIN_REQUIRED
 * PUK_REQUIRED
 * READY
 * UNKNOWN
 * <p>
 * Undocumented:
 * NOT_READY (ICC interface is not ready, e.g. radio is off or powering on)
 * CARD_IO_ERROR (three consecutive times there was a SIM IO error)
 * IMSI (ICC IMSI is ready in property)
 * LOADED (all ICC records, including IMSI, are loaded)
 * <p>
 * Note: some of these are not documented in
 * https://developer.android.com/reference/android/telephony/TelephonyManager.html
 * but they can be found deeper in the source code, namely in com.android.internal.telephony.IccCardConstants.
 */
public class SimStateChangedReceiver extends BroadcastReceiver {

    /**
     * This refers to com.android.internal.telehpony.IccCardConstants.INTENT_KEY_ICC_STATE.
     * It seems not possible to refer it through a builtin class like TelephonyManager, so we
     * define it here manually.
     */
    private static final String EXTRA_SIM_STATE = "ss";

    @Override
    public void onReceive(Context context, Intent intent) {

        String state = intent.getExtras().getString(EXTRA_SIM_STATE);
        if (state == null) {
            return;
        }

        // Do stuff depending on state   
        switch (state) {      
            case "ABSENT": break;
            case "NETWORK_LOCKED": break;
            // etc.
        }
    }
}


Answer 3:

具有的第二种方法PhoneStateListener ,用于监听服务onServiceStateChanged()为我工作。 我相信,在某些设备上,你不会得到内部广播android.intent.action.SIM_STATE_CHANGED



文章来源: How to monitor SIM state change