Android的电话经理来检测SIM卡 [英] Android Telephone manager to detect sim

查看:115
本文介绍了Android的电话经理来检测SIM卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作在Android自动启动应用程序,基本上是依赖于SIM卡的状态。当我的应用程序自动启动,我需要它来检查,​​其中SIM卡已被更改或不。从那以后,我比较当前的SIM卡与以往的SIM通过获取共享preference。但获得新的SIM卡的价值,当应用程序返回一个空指针异常。

I am working on an Android auto-start app that's basically dependent on SIM card state. When my app auto starts I need it to check where the SIM card has been changed or not. After that I compare the current SIM with the past SIM by obtaining the shared preference. But the app returns a null pointer exception when getting the new SIM card's value.

我想SIM卡状态的反应。

I want to react of sim states.

当我收到 SIM_STATE_READY 状态,我想从电话经理获得新的SIM卡状态。

When I receive the SIM_STATE_READY state I want to get the new SIM state from telephone manager.

  telMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
     int simState = telMgr.getSimState();

     switch (simState) 
    {
        case (TelephonyManager.SIM_STATE_ABSENT):
            System.out.println("*******************************************Sim State absent******************************");
            break;
        case (TelephonyManager.SIM_STATE_NETWORK_LOCKED): 
            System.out.println("*******************************************SIM_STATE_NETWORK_LOCKED******************************"+sim);
            break;

        case (TelephonyManager.SIM_STATE_PIN_REQUIRED): 
            System.out.println("*******************************************SIM_STATE_PIN_REQUIRED******************************"+sim);
        break;
        case (TelephonyManager.SIM_STATE_PUK_REQUIRED): 
            System.out.println("*******************************************SIM_STATE_PUK_REQUIRED******************************"+sim);
        break;
        case (TelephonyManager.SIM_STATE_UNKNOWN): 
            System.out.println("*******************************************SIM_STATE_UNKNOWN******************************"+sim);
        break;
        case (TelephonyManager.SIM_STATE_READY): 
        {

        }
    break;
    }
    default: break;
    }

我这样做,但不知道如何监听SIM我想要的状态时,SIM卡已准备好,这样我就可以执行一些code。当装置启动时,它总是返回SIM_STATE_UNKNOWN,并导致程序执行得到完成。

I'm doing this but dont know how to listen for SIM the states I want when the SIM is ready so that I can then execute some code. When the device boots up it always returns "SIM_STATE_UNKNOWN" and causes program's execution got complete.

请帮我实现这一目标。

推荐答案

如果你想做出反应,你需要设置一个监听一个星际变化:

If you want to react to a Sim change you need to set a listener:

final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

    tm.listen(new PhoneStateListener() {
        @Override
        public void onServiceStateChanged(ServiceState serviceState) {
            //Code in here executed on Sim State change
        }

        @Override
        public void onDataConnectionStateChanged(int state) {

        }

这篇关于Android的电话经理来检测SIM卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆