Android Telephony Manager Example |Android App Development With AndroidCource

Sunday, 17 June 2018

Android Telephony Manager Example

Telephony Manager Example in andoid

Telephony Manager provides access to information about the telephony services on the device.Telephony Services such as subscriber id, sim serial number, phone network type etc. Moreover, you can determine the phone state etc.

Add Permission in AndroidManiFest.xml File

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

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="38dp"
        android:layout_marginTop="30dp"
        android:textSize="20dp"
        android:text="Mobile Details:" />

</RelativeLayout>

MainActivity.java

 
package com.example.com.telephonymanager;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.tv_details);

        TelephonyManager device = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {


            return;
        }
        String imei = device.getDeviceId();
        String SimSerialNo = device.getSimSerialNumber();
        String simOperator = device.getSimOperator();
        String NetworkCountryISO=device.getNetworkCountryIso();
        String SIMCountryISO=device.getSimCountryIso();
        String SoftwareVersion=device.getDeviceSoftwareVersion();
        String voiceMailNumber=device.getVoiceMailNumber();

        String text = "Imei : "+ imei + "\n SimSerialNo : " + SimSerialNo + "\n Sim Operator : "
                + simOperator + "\n NetworkCountryISO : " + NetworkCountryISO + 
                "\n SIMCountryISO : " + SIMCountryISO + "\n SoftwareVersion : " + SoftwareVersion +
                "\n voiceMailNumber : " + voiceMailNumber;

        textView.setText(text);

    }
}

0 comments:

Post a Comment