Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 讀取 SIM 卡資訊:取得手機號碼與電信業者

編輯:Android 開發實例

有些 App 在首次啟動時會試著幫使用者把註冊表單的手機號碼填好,這個功能背後靠的是 TelephonyManager。它是 Android 提供讀取通訊服務狀態的入口,透過它可以拿到 IMSI、電信業者、國家代碼等資訊。

本文示範如何包裝一個 SIMCardInfo 類別,把號碼與業者名稱取出來顯示在畫面上。

權限先行

動手之前,記得在 AndroidManifest.xml 補上讀取電話狀態的權限,否則會直接丟 SecurityException:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

(在 Android 6.0 以後的版本,這屬於危險權限,還要另外向使用者動態申請。)

封裝 SIMCardInfo

TelephonyManager 不需要自己 new,用 Context.getSystemService(Context.TELEPHONY_SERVICE) 取得即可。號碼靠 getLine1Number(),業者判斷則是拿 IMSI 的前幾位來比對——460 是中國國家碼,後面兩位才是電信業者。

package com.pei.activity;

import android.content.Context;
import android.telephony.TelephonyManager;

public class SIMCardInfo {

    private TelephonyManager telephonyManager;
    private String IMSI;

    public SIMCardInfo(Context context) {
        telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
    }

    /** 取得目前設定的電話號碼 */
    public String getNativePhoneNumber() {
        return telephonyManager.getLine1Number();
    }

    /** 依 IMSI 前綴判斷電信業者 */
    public String getProvidersName() {
        String ProvidersName = null;
        IMSI = telephonyManager.getSubscriberId();
        // 46000 / 46002 為中國移動,46001 為中國聯通,46003 為中國電信
        if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
            ProvidersName = "中國移動";
        } else if (IMSI.startsWith("46001")) {
            ProvidersName = "中國聯通";
        } else if (IMSI.startsWith("46003")) {
            ProvidersName = "中國電信";
        }
        return ProvidersName;
    }
}

版面配置

畫面很單純,兩個 TextView 顯示結果,一個 Button 觸發查詢:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:gravity="center">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/getSIMInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="獲取手機號" />
</LinearLayout>

Activity 中的呼叫

public class AndroidUtilActivity extends Activity {

    private Button button_getSIMInfo;
    private TextView number;
    private TextView privoid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button_getSIMInfo = (Button) this.findViewById(R.id.getSIMInfo);
        number = (TextView) this.findViewById(R.id.textView1);
        privoid = (TextView) this.findViewById(R.id.textView2);
        button_getSIMInfo.setOnClickListener(new ButtonListener());
    }

    class ButtonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            if (v == button_getSIMInfo) {
                SIMCardInfo siminfo = new SIMCardInfo(AndroidUtilActivity.this);
                number.setText(siminfo.getNativePhoneNumber());
                privoid.setText(siminfo.getProvidersName());
            }
        }
    }
}

實測會踩到的坑

必須老實說:getLine1Number() 在很多機型上會回傳 null。號碼並非寫死在 SIM 卡裡的必填資料,而是由電信業者寫入,部分亞洲門檻的業者根本不寫,加上 Android 版本越來越嚴格地限制隱私讀取,這個方法在近年系統上的可用性相當有限。因此正式專案裡,更可靠的做法是引導使用者手動輸入,或用簡訊驗證碼做確認。

另外 IMSI 前綴判斷法也不保證完整,虛擬運營商(MVNO)號段繁多,硬編碼 if-else 遲早要補。若只是做內部工具或教學 demo,上面的寫法夠用;要上線的話建議把號段表抽成設定檔,日後好維護。

希望這份說明對你的 Android 開發有參考價值。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved