編輯:Android 開發實例
有些 App 在首次啟動時會試著幫使用者把註冊表單的手機號碼填好,這個功能背後靠的是 TelephonyManager。它是 Android 提供讀取通訊服務狀態的入口,透過它可以拿到 IMSI、電信業者、國家代碼等資訊。
本文示範如何包裝一個 SIMCardInfo 類別,把號碼與業者名稱取出來顯示在畫面上。
動手之前,記得在 AndroidManifest.xml 補上讀取電話狀態的權限,否則會直接丟 SecurityException:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
(在 Android 6.0 以後的版本,這屬於危險權限,還要另外向使用者動態申請。)
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>
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 開發有參考價值。
用 WebView 做一個極簡瀏覽器:網址列、跳轉與頁面攔截
WebView 是最快把網頁內容塞進 App 的方式。下面做一個只有網址列、跳轉鈕與網頁區的極簡瀏覽器。 一、程式碼 main.java package com.HHB
Android 登入功能範例:帳號密碼與錯誤次數管理
登入功能看似簡單,卻需要兼顧使用者體驗與安全性。Android 登入畫面通常包含帳號、密碼、登入按鈕,以及錯誤次數提示。 輸入欄位 帳號欄使用一般文字輸入即可;密碼欄則必
Android 進度轉盤使用教學:按鈕觸發載入動畫
進度轉盤,也就是 Spinner,常用於表示不確定完成時間的載入狀態。它會持續旋轉,讓使用者知道程式仍在背景執行。 建立與取得控制項 在 XML 中加入 Progress
封裝 SQLite 分頁表格控件 GVTable:表格區與分頁列的完整實作
前一篇談了 SQLite 的分頁讀取,但那個版本只是把資料印在文字方塊裡。這次往前一步:把「分頁 + 表格呈現」封裝成一個可重複使用的自訂控件。 執行結果: 為什