欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android监控sim卡有没有服务示例(sim卡管理)

程序员文章站 2023-11-14 08:25:52
复制代码 代码如下:public class uniteqservice {public static final string log_tag = "uniteqserv...


复制代码 代码如下:

public class uniteqservice {
public static final string log_tag = "uniteqservice";
public boolean debug = true;
private boolean hasservice = false;//是否有服务
private phonestatelistener[] mphonestatelistener;//监听双卡
private telephonymanager mphone;

//监听类
private phonestatelistener getphonestatelistener(final int sub) {
phonestatelistener sphonestatelistener = new phonestatelistener(sub) {
    @override
        public void onservicestatechanged(servicestate servicestate) {
            // todo auto-generated method stub
            if (servicestate != null) {
                if (servicestate.getstate() == servicestate.state_in_service) {
                    hasservice = true;
                } else {
                    hasservice = false;
                }
            } else {
                if (debug) log.d(tag, "no service state...may be the balance is not enough" + sub);
            }
            super.onservicestatechanged(servicestate);
        }
      };
    return sphonestatelistener;
}
//注册监听
private void registerphonelistener() {
//获取卡号数,msimtelephonymanager 这个类api没有开放,可以通过编译源码
//得到frameworks.jar包并导入你的工程, 然后import android.telephony.msimtelephonymanager;
msub = msimtelephonymanager.getdefault().getphonecount();
mphonestatelistener = new phonestatelistener[msub];
for (int i = 0; i < msub; i++) {
mphonestatelistener[i] = getphonestatelistener(i);
mphone.listen(mphonestatelistener[i], phonestatelistener.listen_service_state);
}
}

//取消监听
private void unregisterphonelistener() {
msub = msimtelephonymanager.getdefault().getphonecount();
for (int i = 0; i < msub; i++) {
if(mphonestatelistener[i] != null){
mphone.listen(mphonestatelistener[i], phonestatelistener.listen_none);
}
}
}
}