Android手机中获取手机号码和运营商信息
01 |
package com.pei.activity; |
02 |
03 |
import android.app.Activity; |
04 |
import android.os.Bundle; |
05 |
import android.view.View; |
06 |
import android.view.View.OnClickListener; |
07 |
import android.widget.Button; |
08 |
import android.widget.TextView; |
09 |
10 |
/** |
11 |
* class name:AndroidUtilActivity<BR> |
12 |
* class description:show get sim card info activity<BR> |
13 |
* PS:注意权限 <BR> |
14 |
* Date:2012-3-12<BR> |
15 |
* @version 1.00 |
16 |
* @author CODYY)peijiangping |
17 |
*/ |
18 |
public class AndroidUtilActivity extends Activity { |
19 |
private Button button_getSIMInfo; |
20 |
private TextView number; |
21 |
private TextView privoid; |
22 |
23 |
@Override |
24 |
public void onCreate(Bundle savedInstanceState) { |
25 |
super.onCreate(savedInstanceState); |
26 |
setContentView(R.layout.main); |
27 |
button_getSIMInfo = (Button) this.findViewById(R.id.getSIMInfo); |
28 |
number = (TextView) this.findViewById(R.id.textView1); |
29 |
privoid = (TextView) this.findViewById(R.id.textView2); |
30 |
button_getSIMInfo.setOnClickListener(new ButtonListener()); |
31 |
} |
32 |
33 |
class ButtonListener implements OnClickListener { |
34 |
35 |
@Override |
36 |
public void onClick(View v) { |
37 |
if (v == button_getSIMInfo) { |
38 |
SIMCardInfo siminfo = new SIMCardInfo(AndroidUtilActivity.this); |
39 |
System.out.println(siminfo.getProvidersName()); |
40 |
System.out.println(siminfo.getNativePhoneNumber()); |
41 |
number.setText(siminfo.getNativePhoneNumber()); |
42 |
privoid.setText(siminfo.getProvidersName()); |
43 |
} |
44 |
} |
45 |
46 |
} |
47 |
} |
01 |
package com.pei.activity; |
02 |
03 |
import android.content.Context; |
04 |
import android.telephony.TelephonyManager; |
05 |
06 |
/** |
07 |
* class name:SIMCardInfo<BR> |
08 |
* class description:读取Sim卡信息<BR> |
09 |
* PS: 必须在加入各种权限 <BR> |
10 |
* Date:2012-3-12<BR> |
11 |
* |
12 |
* @version 1.00 |
13 |
* @author CODYY)peijiangping |
14 |
*/ |
15 |
public class SIMCardInfo { |
16 |
/** |
17 |
* TelephonyManager提供设备上获取通讯服务信息的入口。 应用程序可以使用这个类方法确定的电信服务商和国家 以及某些类型的用户访问信息。 |
18 |
* 应用程序也可以注册一个监听器到电话收状态的变化。不需要直接实例化这个类 |
19 |
* 使用Context.getSystemService(Context.TELEPHONY_SERVICE)来获取这个类的实例。 |
20 |
*/ |
21 |
private TelephonyManager telephonyManager; |
22 |
/** |
23 |
* 国际移动用户识别码 |
24 |
*/ |
25 |
private String IMSI; |
26 |
27 |
public SIMCardInfo(Context context) { |
28 |
telephonyManager = (TelephonyManager) context |
29 |
.getSystemService(Context.TELEPHONY_SERVICE); |
30 |
} |
31 |
32 |
/** |
33 |
* Role:获取当前设置的电话号码 |
34 |
* <BR>Date:2012-3-12 |
35 |
* <BR>@author CODYY)peijiangping |
36 |
*/ |
37 |
public String getNativePhoneNumber() { |
38 |
String NativePhoneNumber=null; |
39 |
NativePhoneNumber=telephonyManager.getLine1Number(); |
40 |
return NativePhoneNumber; |
41 |
} |
42 |
43 |
/** |
44 |
* Role:Telecom service providers获取手机服务商信息 <BR> |
45 |
* 需要加入权限<uses-permission |
46 |
* android:name="android.permission.READ_PHONE_STATE"/> <BR> |
47 |
* Date:2012-3-12 <BR> |
48 |
* |
49 |
* @author CODYY)peijiangping |
50 |
*/ |
51 |
public String getProvidersName() { |
52 |
String ProvidersName = null; |
53 |
// 返回唯一的用户ID;就是这张卡的编号神马的 |
54 |
IMSI = telephonyManager.getSubscriberId(); |
55 |
// IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。 |
56 |
System.out.println(IMSI); |
57 |
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) { |
58 |
ProvidersName = "中国移动"; |
59 |
} else if (IMSI.startsWith("46001")) { |
60 |
ProvidersName = "中国联通"; |
61 |
} else if (IMSI.startsWith("46003")) { |
62 |
ProvidersName = "中国电信"; |
63 |
} |
64 |
return ProvidersName; |
65 |
} |
66 |
} |
01 |
<?xml version="1.0" encoding="utf-8"?> |
02 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
03 |
android:layout_width="fill_parent" |
04 |
android:layout_height="fill_parent" |
05 |
android:orientation="vertical" android:gravity="center"> |
06 |
07 |
<TextView |
08 |
android:id="@+id/textView1" |
09 |
android:layout_width="wrap_content" |
10 |
android:layout_height="wrap_content" |
11 |
android:text="TextView" /> |
12 |
13 |
<TextView |
14 |
android:id="@+id/textView2" |
15 |
android:layout_width="wrap_content" |
16 |
android:layout_height="wrap_content" |
17 |
android:text="TextView" /> |
18 |
19 |
<Button |
20 |
android:id="@+id/getSIMInfo" |
21 |
android:layout_width="wrap_content" |
22 |
android:layout_height="wrap_content" |
23 |
android:text="获取手机号码等信息" /> |
24 |
25 |
</LinearLayout> |


浙公网安备 33010602011771号