加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 23:37:41 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 /* 监听sim状态改变的广播,返回sim卡的状态, 有效或者无效。 双卡中只要有一张卡的状态有效即返回状态为有效,两张卡都无效则返回无效。 */ import

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

/* 
     监听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;  
            }  
        }  
    }  
  
}  

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读