Blog Details

img
Design

What is telephony manager in Android

Spoke Right / 19 Nov, 2023

The info about the telephony services, including the subscriber id, sim serial number, phone network type, phone state, etc., are facilitated by the android.telephony.TelephonyManager class. The TelephonyManager class of Android can also be used to get the information of the call state. The listen method of the TelephonyManager class is used to serve this purpose, bypassing the PhonStateListener instance (which is necessary to implement to get the Call State).

Android TelephonyManager Example:

In the below example, we are demonstrating the usage of the TelephonyManager to display the information of the telephony services.

activity_main.xml:

In the activity_main.xml file, we will only drag a TextView from the palette.



    

Activity class:(File: MainActivity.java)

In the MainActivity.java file, we will write the code to display the information about the telephony services.

package com.example.radioapp;
 
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.widget.TextView;
 
import static android.Manifest.permission.READ_PHONE_NUMBERS;
import static android.Manifest.permission.READ_PHONE_STATE;
import static android.Manifest.permission.READ_SMS;
 
public class MainActivity extends AppCompatActivity {
    private static final int PERMISSION_REQUEST_CODE = 100;
    TextView textView;
    TelephonyManager telephonyManager;
 
    @RequiresApi(api = Build.VERSION_CODES.P)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.text);
        telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, READ_SMS) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, READ_PHONE_NUMBERS) !=
                        PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
                READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{READ_SMS, READ_PHONE_NUMBERS, READ_PHONE_STATE}, PERMISSION_REQUEST_CODE);
        } else {
            textView.setText("IMEI No. "+telephonyManager.getImei());
        }
    }

AndroidManifest.xml:

In the AndroidManifest.xml file, we will add the code to provide the READ_PHONE_STATE

permission.



 
    
    
    
        
            
                
                
            
        
    

0 comments

Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0