Copy Code code as follows:
public class Uniteqservice {
public static final String Log_tag = "Uniteqservice";
public Boolean DEBUG = true;
Private Boolean hasservice = false;//whether there is a service
Private phonestatelistener[] mphonestatelistener;//monitor dual SIM card
Private Telephonymanager Mphone;
Listening class
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 are not enough" + sub);
}
Super.onservicestatechanged (servicestate);
}
};
return sphonestatelistener;
}
Registering for listening
private void Registerphonelistener () {
Get the card numbers, Msimtelephonymanager This class API is not open, you can compile the source code
Get the Frameworks.jar package and import your project, then 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);
}
}
Cancel Listening
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);
}
}
}
}