Android手機中擷取手機號碼和電訊廠商資訊

來源:互聯網
上載者:User

代碼如下:

package com.pei.activity;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;/** * class name:AndroidUtilActivity<BR> * class description:show get sim card info activity<BR> * PS:注意許可權 <BR> * Date:2012-3-12<BR> * @version 1.00 * @author CODYY)peijiangping */public class AndroidUtilActivity extends Activity {private Button button_getSIMInfo;private TextView number;private TextView privoid;@Overridepublic 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 {@Overridepublic void onClick(View v) {if (v == button_getSIMInfo) {SIMCardInfo siminfo = new SIMCardInfo(AndroidUtilActivity.this);System.out.println(siminfo.getProvidersName());System.out.println(siminfo.getNativePhoneNumber());number.setText(siminfo.getNativePhoneNumber());privoid.setText(siminfo.getProvidersName());}}}}

package com.pei.activity;import android.content.Context;import android.telephony.TelephonyManager;/** * class name:SIMCardInfo<BR> * class description:讀取Sim卡資訊<BR> * PS: 必須在加入各種許可權 <BR> * Date:2012-3-12<BR> *  * @version 1.00 * @author CODYY)peijiangping */public class SIMCardInfo {/** * TelephonyManager提供裝置上擷取通訊服務資訊的入口。 應用程式可以使用這個類方法確定的電信服務商和國家 以及某些類型的使用者訪問資訊。 * 應用程式也可以註冊一個監聽器到電話收狀態的變化。不需要直接執行個體化這個類 * 使用Context.getSystemService(Context.TELEPHONY_SERVICE)來擷取這個類的執行個體。 */private TelephonyManager telephonyManager;/** * 國際移動使用者識別碼 */private String IMSI;public SIMCardInfo(Context context) {telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);}/** * Role:擷取當前設定的電話號碼 * <BR>Date:2012-3-12 * <BR>@author CODYY)peijiangping */public String getNativePhoneNumber() {String NativePhoneNumber=null;NativePhoneNumber=telephonyManager.getLine1Number();return NativePhoneNumber;}/** * Role:Telecom service providers擷取手機服務商資訊 <BR> * 需要加入許可權<uses-permission * android:name="android.permission.READ_PHONE_STATE"/> <BR> * Date:2012-3-12 <BR> *  * @author CODYY)peijiangping */public String getProvidersName() {String ProvidersName = null;// 返回唯一的使用者ID;就是這張卡的編號神馬的IMSI = telephonyManager.getSubscriberId();// IMSI號前面3位460是國家,緊接著後面2位00 02是中國移動,01是中國聯通,03是中國電信。System.out.println(IMSI);if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {ProvidersName = "中國移動";} else if (IMSI.startsWith("46001")) {ProvidersName = "中國聯通";} else if (IMSI.startsWith("46003")) {ProvidersName = "中國電信";}return ProvidersName;}}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://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>

圖片如下:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.