android監控SIM卡狀態的廣播示例代碼

b5pp 9年前發布 | 2K 次閱讀 Java Android

/ 
     監聽sim狀態改變的廣播,返回sim卡的狀態, 有效或者無效。 
    雙卡中只要有一張卡的狀態有效即返回狀態為有效,兩張卡都無效則返回無效。 
 /
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

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;  
        }  
    }  
}  

} </pre>

 本文由用戶 b5pp 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!