Android realizes the function of automatic recording of incoming and outgoing calls

When we use Android mobile phones to make calls, we may sometimes need to automatically record incoming and outgoing calls. This article will explain in detail the method to realize the automatic recording of Android incoming and outgoing calls. You can complete this function by writing a program according to the method in this article.

The key to automatic recording of incoming and outgoing calls is how to monitor the change of mobile phone status:

1) The status conversion of incoming call is as follows (the red mark is the status we want to use)

Idle ― > ringing ― > active ― > hanging up ― > idle

Or IDL - > ringing - > reject - > IDL

2) The switching of de energized state is as follows:

Idle - > dialing - > ringing - > active - > hanging up - > idle

Or IDL - > dialing - > ringing - > hanging up / rejecting - > IDL

The following two states are analyzed and implemented respectively.

1. First line call analysis and implementation.

Compared with power off, the change detection of incoming state is simpler. The phonestatelistener class in Android API provides corresponding methods, but we need to override the oncallstatechanged (int state, string incomingnumber) method to detect the incoming call state, and add the recording function on this basis. The state parameter refers to various phone states. At that time, we will compare it with the states we want to use below. If the phone is in the state we want, we will carry out a series of operations, otherwise we will ignore it. To obtain these statuses, we also need another telephone related class, that is, telephonymanager. This class provides some telephone statuses, of which we need to use: telephonymanager CALL_ STATE_ Idle, telephonymanager CALL_ STATE_ Offhook and telephonymanager CALL_ STATE_ Ring these three states. Judging these three states, you can inherit Android telephony. The phonestatelistener class implements the oncallstatechanged (int state, string incomingnumber) method mentioned above. Please see the following code:

The above incoming call status monitoring code is not enough to realize the monitoring function. We also need to implement the monitoring in one of our activities or services. The method is very simple. The code is as follows:

In this way, the incoming call status monitoring function is realized, but it is not enough to run in the device. It also needs two permissions to obtain the mobile phone status:

So you can run.

Speaking of this, I think if you can realize the recording function, there should be no problem to realize the automatic recording of incoming calls on this basis, but please allow me to be brief and wordy. Since it's an incoming call, if you want to record, you should be listening to the telephony manager CALL_ STATE_ In the offhook state, turn on the recorder to start recording, and listen to the telephonymanager CALL_ STATE_ In idle state, turn off the recorder and stop recording. In this way, the incoming call recording function is completed. Don't forget that the recording function also needs permission:

2. After introducing the automatic recording of incoming calls, let's introduce the implementation method of automatic recording of outgoing calls.

As mentioned above, compared with the monitoring of incoming call status, the power off is more troublesome, and even this method is not universal. This is mainly because the corresponding classes and methods for power off status monitoring are not provided in the Android API (maybe I just contacted and didn't find them). At the beginning, I searched the Internet and didn't find the corresponding solution. Most of them were caller monitoring, that is, the above method. However, I found a blog post halfway (later I couldn't find it). I remember the way to query the system log to find the keywords of various states in the power removal process. In desperation, he finally compromised this method.

The contents of my log (on Lenovo A65) are as follows:

The filtering keyword is mforeground

Filter keyword mbackground

It can be seen from the above log that the capitalized English word at the end of each line is the power off state. The state description is as follows:

Since I dialed 10010, there was no ringing process (the computer automatically connected fast enough), and there was a missing status. The status was alerting, which is the status that the other party is ringing.

It's easy to do with these power off states. Now we have to read the system log and find these states. The extracted keywords are mforeground and mbackground (which may be generated by different devices. According to their specific device settings, only the foreground ones are extracted here), If the read log line contains mforground, check whether it contains the above status words. In that case, look at the code that reads the system log.

In the above code, threads are used to prevent blocking the execution of other methods of the main method during log reading, which affects the program to capture the corresponding phone state.

Well, after capturing the changes of various states in the power off process, how to notify the program? My method is to send a broadcast to the system immediately after capturing, and then the program receives the broadcast, and then process the recording event after receiving it. To send a broadcast, you need to send a unique broadcast. To do this, create the following classes:

The program needs to read the system log:

XML / HTML code

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

Then, send a broadcast where each power off state is detected in the class reading the log. Then, the complete code of reading the log is as follows:

If a broadcast is sent, there must be a receiver. The receiver is defined as follows (the code about the recorder can be ignored first):

There is such a code:

Here is the broadcast sent by the receiving system for receiving the power off broadcast. In this way, the de energized state is obtained.

3. With the above main codes, it can be said that the incoming and outgoing power monitoring function is completed. Next, create a service to run monitoring:

Register the following services:

XML / HTML code

<service android:name="com.sdvdxl.service.PhoneCallStateService" />

So far, the monitoring function of incoming and outgoing power status has been completed. There is one recorder left with the recorder code as follows:

At this point, all functions of automatic recording of incoming and outgoing calls are completed. You can try to write and implement them yourself.

The above is the development of Android phone recording. I hope to help friends in need. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>