Android's method for obtaining mobile phone Numbers and carrier information

  • 2020-06-15 10:10:45
  • OfStack

This article illustrates Android's method for obtaining mobile phone Numbers and carrier information. Share to everybody for everybody reference. Specific implementation methods are 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 : Notice permissions <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 Reading: Sim Card information <BR>
 * PS : Must be in addition to various permissions <BR>
 * Date:2012-3-12<BR>
 * 
 * @version 1.00
 * @author CODYY)peijiangping
 */ 
public class SIMCardInfo { 
    /**
     * TelephonyManager Provides an entry point on the device to obtain communication service information. The application can use this class method to identify the telecommunications provider and country And certain types of user access information.
     * Applications can also be registered 1 Changes in the status of a listener to a call. You do not need to instantiate this class directly
     * use Context.getSystemService(Context.TELEPHONY_SERVICE) To get an instance of this class.
     */ 
    private TelephonyManager telephonyManager; 
    /**
     * International Mobile User Identification code
     */ 
    private String IMSI; 
 
    public SIMCardInfo(Context context) { 
        telephonyManager = (TelephonyManager) context 
                .getSystemService(Context.TELEPHONY_SERVICE); 
    } 
 
    /**
     * Role: Gets the phone number for the current setting
     * <BR>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 cell phone service provider information <BR>
     * You need to add 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; 
        // Return only 1 The user ID; What's the number of this card  
        IMSI = telephonyManager.getSubscriberId(); 
        // IMSI In front of the no. 3 position 460 It's the country, right after that 2 position 00 02 It's China Mobile, 01 It's China Unicom, 03 It's China 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; 
    } 
}

<?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 has been helpful for your Android programming.


Related articles: