SHARE

We learn about android.telephony.TelephonyManager by creating a simple example:

Step 1: Create a new project
Step 2: with file main_activity.xml

add some TextView and Button:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TUTORIALSPOTS.COM"
        android:textColor="#ff0000"
        android:textSize="26dp"
        android:id="@+id/textView"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Network Operator Name"
        android:id="@+id/btn"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="77dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Manufacturer name"
        android:id="@+id/button"
        android:layout_below="@+id/button3"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Phone model"
        android:id="@+id/button2"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Service Provider Name"
        android:id="@+id/button3"
        android:layout_below="@+id/btn"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Sim State"
        android:id="@+id/button4"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Sim Number"
        android:id="@+id/button5"
        android:layout_below="@+id/button4"
        android:layout_centerHorizontal="true" />

android telephony manager

Step 3: With file MainActivity.java

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    public Button btn;
    public Button button;
    public Button button2;
    public Button button3;
    public Button button4;
    public Button button5;
    public String carrierName;
    public String manufacturer;
    public String model;
    public String spn;
    public int simState;
    public String simNumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        btn = (Button) findViewById(R.id.btn);
        button = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        button5 = (Button) findViewById(R.id.button5);

        // Get System TELEPHONY service reference
        TelephonyManager telephonyManager = (TelephonyManager) getBaseContext()
                .getSystemService(Context.TELEPHONY_SERVICE);

        // Get carrier name (Network Operator Name)
        carrierName = telephonyManager.getNetworkOperatorName();
        // Get Service Provider Name
        spn = telephonyManager.getSimOperatorName();
        // Get Phone model and manufacturer name
        manufacturer = Build.MANUFACTURER;
        model = Build.MODEL;
        // Get sim state
        simState = telephonyManager.getSimState();

        // Get Sim number
        simNumber = telephonyManager.getLine1Number();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), carrierName, Toast.LENGTH_LONG).show();
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), manufacturer, Toast.LENGTH_LONG).show();
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), model, Toast.LENGTH_LONG).show();
            }
        });

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), spn, Toast.LENGTH_LONG).show();
            }
        });

        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), String.valueOf(simState), Toast.LENGTH_LONG).show();
            }
        });

        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), simNumber, Toast.LENGTH_LONG).show();
            }
        });
    }
...
}

Step 4: file AndroidManifest.xml

add

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Step 5: run, and we get the result:
android get service provider name

So, we can get service provider name, network operator name, phone manufacturer name, phone model and much…

Get service provider name:

spn = telephonyManager.getSimOperatorName();

Get network operator name:

carrierName = telephonyManager.getNetworkOperatorName();

NO COMMENTS