Android access to mobile phone numbers and carrier information _android

Source: Internet
Author: User

This article gives an example of how Android obtains mobile phone numbers and carrier information. Share to everyone for your reference. The implementation methods are as follows:

Copy Code code as follows:
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: Attention rights <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 ());
}
}

}
}

Copy Code code as follows:
Package com.pei.activity;

Import Android.content.Context;
Import Android.telephony.TelephonyManager;

/**
* Class Name:simcardinfo<br>
* Class Description: Read SIM card information <BR>
* PS: Must be included in various permissions <BR>
* date:2012-3-12<br>
*
* @version 1.00
* @author codyy) peijiangping
*/
public class Simcardinfo {
/**
* Telephonymanager provides access to information on communications services on the equipment. Applications can use this class of methods to determine the telecommunications provider and the state, as well as certain types of user access information.
* Applications can also register a listener to a change in the phone's receiving state. No direct instantiation of this class is required
* Use Context.getsystemservice (context.telephony_service) to obtain an instance of this class.
*/
Private Telephonymanager Telephonymanager;
/**
* International Mobile Subscriber Identification code
*/
Private String IMSI;

Public Simcardinfo {
Telephonymanager = (Telephonymanager) context
. Getsystemservice (Context.telephony_service);
}

/**
* Role: Get the phone number currently set
* &LT;BR&GT;DATE:2012-3-12
* <BR> @author Codyy) peijiangping
*/
Public String Getnativephonenumber () {
String Nativephonenumber=null;
Nativephonenumber=telephonymanager.getline1number ();
return nativephonenumber;
}

/**
* Role:telecom service providers access to mobile phone service provider Information <BR>
* Need to join permissions <uses-permission
* android:name= "Android.permission.READ_PHONE_STATE"/> <BR>
* Date:2012-3-12 <BR>
*
* @author codyy) peijiangping
*/
Public String Getprovidersname () {
String providersname = null;
Returns a unique user ID; that's the number of the card. God horse's
IMSI = Telephonymanager.getsubscriberid ();
The IMSI 3-digit 460 is the country, followed by 2 digits 00 02 is China Mobile, 01 is China Unicom, 03 is Chinese telecom.
System.out.println (IMSI);
if (Imsi.startswith ("46000") | | Imsi.startswith ("46002")) {
Providersname = "China Mobile";
else if (Imsi.startswith ("46001")) {
Providersname = "China Unicom";
else if (Imsi.startswith ("46003")) {
Providersname = "China Telecom";
}
return providersname;
}
}

Copy Code code as follows:
<?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= "Get cell phone number"/>

I hope this article will help you with your Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.