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

jopen 12年前

代码如下:

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;     @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);      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>
图片如下:

转自:http://blog.csdn.net/peijiangping1989/article/details/7344925