Android手机中获取手机号码和运营商信息

时间:2023-03-09 17:11:08
Android手机中获取手机号码和运营商信息

代码如下:

  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>

图片如下:
Android手机中获取手机号码和运营商信息