Get the App
SLTechnology News&Howtos  ›  Development  › 

How to monitor calls in Android

Shulou Source: shulou.com Published: 2022-05-31 21:46:21 11月06日 Update

Most people do not understand the knowledge points of this article "how to monitor calls in Android", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this "how to monitor calls in Android" article.

TelephonyManager serves as a Service interface for users to query phone-related content, such as IMEI,LineNumber1, etc. An example of TelephonyManager can be obtained from the following code.

TelephonyManager mTelephonyMgr = (TelephonyManager) this .getSystemService (Context.TELEPHONY_SERVICE)

In the Android platform, PhoneStateListener is a very useful listener to monitor the status of the phone, such as call status and connection services. The method for Android to monitor calls is as follows:

Public void onCallForwardingIndicatorChanged (boolean cfi)

Public void onCallStateChanged (int state

String incomingNumber)

Public void onCellLocationChanged (CellLocation location)

Public void onDataActivity (int direction)

Public void onDataConnectionStateChanged (int state)

Public void onMessageWaitingIndicatorChanged (boolean mwi)

Public void onServiceStateChanged

(ServiceState serviceState)

Public void onSignalStrengthChanged (int asu)

Here we only need to override the onCallStateChanged () method to monitor the call status. Three states are defined in TelephonyManager: ring (RINGING), off-hook (OFFHOOK) and idle (IDLE). We know the current phone status by the value of state.

After you have obtained the TelephonyManager interface, you can call the listen () method to monitor the call by Android.

MTelephonyMgr.listen (new TeleListener (), PhoneStateListener.LISTEN_CALL_STATE)

The following is a simple test example that simply appends the call status to TextView.

Package com.j2medev

Import android.app.Activity

Import android.content.Context

Import android.os.Bundle

Import android.telephony.PhoneStateListener

Import android.telephony.TelephonyManager

Import android.util.Log

Import android.widget.TextView

Public class Telephony extends Activity {

Private static final String TAG = "Telephony"

TextView view = null

@ Override

Protected void onCreate (Bundle savedInstanceState) {

Super.onCreate (savedInstanceState)

TelephonyManager mTelephonyMgr = (TelephonyManager) this

.getSystemService (Context.TELEPHONY_SERVICE)

MTelephonyMgr.listen (new TeleListener ()

PhoneStateListener.LISTEN_CALL_STATE)

View = new TextView (this)

View.setText ("listen the state of phone\ n")

SetContentView (view)

}

Class TeleListener extends PhoneStateListener {

@ Override

Public void onCallStateChanged (int state

String incomingNumber) {

Super.onCallStateChanged (state, incomingNumber)

Switch (state) {

Case TelephonyManager.CALL_STATE_IDLE: {

Log.e (TAG, "CALL_STATE_IDLE")

View.append ("CALL_STATE_IDLE" + "\ n")

Break

}

Case TelephonyManager.CALL_STATE_OFFHOOK: {

Log.e (TAG, "CALL_STATE_OFFHOOK")

View.append ("CALL_STATE_OFFHOOK" + "\ n")

Break

}

Case TelephonyManager.CALL_STATE_RINGING: {

Log.e (TAG, "CALL_STATE_RINGING")

View.append ("CALL_STATE_RINGING" + "\ n")

Break

}

Default:

Break

}

}

}

}

Don't forget to add a permission to AndroidManifest.xml.

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

The above is about the content of this article on "how to monitor calls in Android". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Monitoring content status method phone call interface article knowledge article code value example just most of the examples that is the platform come on ring Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info Huawei Xiaomi MySQL