Android获取手机号码和运营商信息的方法

所属分类: 软件编程 / Android 阅读数: 475
收藏 0 赞 0 分享

本文实例讲述了Android获取手机号码和运营商信息的方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:
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="获取手机号" />

希望本文所述对大家的Android程序设计有所帮助。

更多精彩内容其他人还在看

android byte[] 和short[]转换的方法代码

这篇文章主要介绍了android byte[] 和short[]转换的方法代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取应用程序大小的方法

这篇文章主要介绍了Android获取应用程序大小的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取其他包的Context实例代码

这篇文章主要介绍了Android获取其他包的Context实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android放大镜的实现代码

这篇文章主要介绍了Android放大镜的实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android 读取Properties配置文件的小例子

这篇文章主要介绍了Android 读取Properties配置文件的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android通讯录开发之删除功能的实现方法

这篇文章主要介绍了Android通讯录开发之删除功能的实现方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享
查看更多