Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to monitor calls in Android

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report