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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="left" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="110dp"
        android:layout_marginTop="200dp"
        android:text="TextView" />


    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="76dp"
        android:text="TextView" />


    <Button
        android:id="@+id/getSIMInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView2"
        android:layout_marginLeft="94dp"
        android:layout_marginTop="59dp"
        android:text="@string/get_phone_no_info" />


</RelativeLayout>



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android_sls"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />
    
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.sls.AndroidUtilActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
    </application>


</manifest>



package com.sls;


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;


import com.android_sls.R;
import com.common.SIMCardInfo;


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.phone_no);


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.common;


import android.content.Context;
import android.telephony.TelephonyManager;


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;
}


}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值