歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android手機中獲取手機號碼和運營商信息

Android手機中獲取手機號碼和運營商信息

日期:2017/3/1 10:26:49   编辑:Linux編程

代碼如下:

  1. package com.pei.activity;
  2. import Android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. /**
  9. * class name:AndroidUtilActivity<BR>
  10. * class description:show get sim card info activity<BR>
  11. * PS:注意權限 <BR>
  12. * Date:2012-3-12<BR>
  13. * @version 1.00
  14. * @author CODYY)peijiangping
  15. */
  16. public class AndroidUtilActivity extends Activity {
  17. private Button button_getSIMInfo;
  18. private TextView number;
  19. private TextView privoid;
  20. @Override
  21. public void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.main);
  24. button_getSIMInfo = (Button) this.findViewById(R.id.getSIMInfo);
  25. number = (TextView) this.findViewById(R.id.textView1);
  26. privoid = (TextView) this.findViewById(R.id.textView2);
  27. button_getSIMInfo.setOnClickListener(new ButtonListener());
  28. }
  29. class ButtonListener implements OnClickListener {
  30. @Override
  31. public void onClick(View v) {
  32. if (v == button_getSIMInfo) {
  33. SIMCardInfo siminfo = new SIMCardInfo(AndroidUtilActivity.this);
  34. System.out.println(siminfo.getProvidersName());
  35. System.out.println(siminfo.getNativePhoneNumber());
  36. number.setText(siminfo.getNativePhoneNumber());
  37. privoid.setText(siminfo.getProvidersName());
  38. }
  39. }
  40. }
  41. }

  1. package com.pei.activity;
  2. import android.content.Context;
  3. import android.telephony.TelephonyManager;
  4. /**
  5. * class name:SIMCardInfo<BR>
  6. * class description:讀取Sim卡信息<BR>
  7. * PS: 必須在加入各種權限 <BR>
  8. * Date:2012-3-12<BR>
  9. *
  10. * @version 1.00
  11. * @author CODYY)peijiangping
  12. */
  13. public class SIMCardInfo {
  14. /**
  15. * TelephonyManager提供設備上獲取通訊服務信息的入口。 應用程序可以使用這個類方法確定的電信服務商和國家 以及某些類型的用戶訪問信息。
  16. * 應用程序也可以注冊一個監聽器到電話收狀態的變化。不需要直接實例化這個類
  17. * 使用Context.getSystemService(Context.TELEPHONY_SERVICE)來獲取這個類的實例。
  18. */
  19. private TelephonyManager telephonyManager;
  20. /**
  21. * 國際移動用戶識別碼
  22. */
  23. private String IMSI;
  24. public SIMCardInfo(Context context) {
  25. telephonyManager = (TelephonyManager) context
  26. .getSystemService(Context.TELEPHONY_SERVICE);
  27. }
  28. /**
  29. * Role:獲取當前設置的電話號碼
  30. * <BR>Date:2012-3-12
  31. * <BR>@author CODYY)peijiangping
  32. */
  33. public String getNativePhoneNumber() {
  34. String NativePhoneNumber=null;
  35. NativePhoneNumber=telephonyManager.getLine1Number();
  36. return NativePhoneNumber;
  37. }
  38. /**
  39. * Role:Telecom service providers獲取手機服務商信息 <BR>
  40. * 需要加入權限<uses-permission
  41. * android:name="android.permission.READ_PHONE_STATE"/> <BR>
  42. * Date:2012-3-12 <BR>
  43. *
  44. * @author CODYY)peijiangping
  45. */
  46. public String getProvidersName() {
  47. String ProvidersName = null;
  48. // 返回唯一的用戶ID;就是這張卡的編號神馬的
  49. IMSI = telephonyManager.getSubscriberId();
  50. // IMSI號前面3位460是國家,緊接著後面2位00 02是中國移動,01是中國聯通,03是中國電信。
  51. System.out.println(IMSI);
  52. if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
  53. ProvidersName = "中國移動";
  54. } else if (IMSI.startsWith("46001")) {
  55. ProvidersName = "中國聯通";
  56. } else if (IMSI.startsWith("46003")) {
  57. ProvidersName = "中國電信";
  58. }
  59. return ProvidersName;
  60. }
  61. }

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" android:gravity="center">
  6. <TextView
  7. android:id="@+id/textView1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="TextView" />
  11. <TextView
  12. android:id="@+id/textView2"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="TextView" />
  16. <Button
  17. android:id="@+id/getSIMInfo"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="獲取手機號碼等信息" />
  21. </LinearLayout>
圖片如下:

Copyright © Linux教程網 All Rights Reserved