Android獲取手機號碼和運營商信息的方法
更新時間:2015年01月20日 08:59:10 投稿:shichen2014
這篇文章主要介紹了Android獲取手機號碼和運營商信息的方法,以實例形式完整講述了獲取手機號碼和運營商信息的技巧,代碼中包含完整的注釋說明,需要的朋友可以參考下
本文實例講述了Android獲取手機號碼和運營商信息的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
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:注意權(quán)限 <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());
}
}
}
}
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:注意權(quán)限 <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());
}
}
}
}
復(fù)制代碼 代碼如下:
package com.pei.activity;
import android.content.Context;
import android.telephony.TelephonyManager;
/**
* class name:SIMCardInfo<BR>
* class description:讀取Sim卡信息<BR>
* PS: 必須在加入各種權(quán)限 <BR>
* Date:2012-3-12<BR>
*
* @version 1.00
* @author CODYY)peijiangping
*/
public class SIMCardInfo {
/**
* TelephonyManager提供設(shè)備上獲取通訊服務(wù)信息的入口。 應(yīng)用程序可以使用這個類方法確定的電信服務(wù)商和國家 以及某些類型的用戶訪問信息。
* 應(yīng)用程序也可以注冊一個監(jiān)聽器到電話收狀態(tài)的變化。不需要直接實例化這個類
* 使用Context.getSystemService(Context.TELEPHONY_SERVICE)來獲取這個類的實例。
*/
private TelephonyManager telephonyManager;
/**
* 國際移動用戶識別碼
*/
private String IMSI;
public SIMCardInfo(Context context) {
telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
}
/**
* Role:獲取當(dāng)前設(shè)置的電話號碼
* <BR>Date:2012-3-12
* <BR>@author CODYY)peijiangping
*/
public String getNativePhoneNumber() {
String NativePhoneNumber=null;
NativePhoneNumber=telephonyManager.getLine1Number();
return NativePhoneNumber;
}
/**
* Role:Telecom service providers獲取手機服務(wù)商信息 <BR>
* 需要加入權(quán)限<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是中國聯(lián)通,03是中國電信。
System.out.println(IMSI);
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
ProvidersName = "中國移動";
} else if (IMSI.startsWith("46001")) {
ProvidersName = "中國聯(lián)通";
} else if (IMSI.startsWith("46003")) {
ProvidersName = "中國電信";
}
return ProvidersName;
}
}
import android.content.Context;
import android.telephony.TelephonyManager;
/**
* class name:SIMCardInfo<BR>
* class description:讀取Sim卡信息<BR>
* PS: 必須在加入各種權(quán)限 <BR>
* Date:2012-3-12<BR>
*
* @version 1.00
* @author CODYY)peijiangping
*/
public class SIMCardInfo {
/**
* TelephonyManager提供設(shè)備上獲取通訊服務(wù)信息的入口。 應(yīng)用程序可以使用這個類方法確定的電信服務(wù)商和國家 以及某些類型的用戶訪問信息。
* 應(yīng)用程序也可以注冊一個監(jiān)聽器到電話收狀態(tài)的變化。不需要直接實例化這個類
* 使用Context.getSystemService(Context.TELEPHONY_SERVICE)來獲取這個類的實例。
*/
private TelephonyManager telephonyManager;
/**
* 國際移動用戶識別碼
*/
private String IMSI;
public SIMCardInfo(Context context) {
telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
}
/**
* Role:獲取當(dāng)前設(shè)置的電話號碼
* <BR>Date:2012-3-12
* <BR>@author CODYY)peijiangping
*/
public String getNativePhoneNumber() {
String NativePhoneNumber=null;
NativePhoneNumber=telephonyManager.getLine1Number();
return NativePhoneNumber;
}
/**
* Role:Telecom service providers獲取手機服務(wù)商信息 <BR>
* 需要加入權(quán)限<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是中國聯(lián)通,03是中國電信。
System.out.println(IMSI);
if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
ProvidersName = "中國移動";
} else if (IMSI.startsWith("46001")) {
ProvidersName = "中國聯(lián)通";
} else if (IMSI.startsWith("46003")) {
ProvidersName = "中國電信";
}
return ProvidersName;
}
}
復(fù)制代碼 代碼如下:
<?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 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程序設(shè)計有所幫助。
相關(guān)文章
Android使用ViewStub實現(xiàn)布局優(yōu)化方法示例
這篇文章主要為大家介紹了Android使用ViewStub實現(xiàn)布局優(yōu)化方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Android仿IOS ViewPager滑動進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android仿IOS ViewPager滑動進(jìn)度條的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
實現(xiàn)Android studio設(shè)置自動導(dǎo)包及自動導(dǎo)包快捷鍵
這篇文章主要介紹了實現(xiàn)Android studio設(shè)置自動導(dǎo)包及自動導(dǎo)包快捷鍵的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android使用MulticastSocket實現(xiàn)多點廣播圖片
這篇文章主要為大家詳細(xì)介紹了Android使用MulticastSocket實現(xiàn)多點廣播圖片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01

