Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android監控sim卡有沒有服務示例(sim卡管理)

android監控sim卡有沒有服務示例(sim卡管理)

編輯:關於Android編程


復制代碼 代碼如下:
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);
}
}
}
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved