SoFunction
Updated on 2025-03-02

Solution to parse the phone status in Android games to pause or continue the game

Friends who know some of the smartphones know one of the widely used mobile phone operating systems Android open source mobile phone operating systems. So how should we operate if we want to implement the call monitoring function in this system? Here I will introduce the relevant implementation methods of Android monitoring calls in detail.

When developing an application, we hope to be able to monitor incoming calls on the phone in order to perform operations such as pausing the music player. When the phone is over, playback will be resumed again. This task can be accomplished through TelephonyManager and PhoneStateListener on the Android platform.
TelephonyManager is a Service interface to provide users with information about phone-related content, such as IMEI, LineNumber1, etc. You can get an instance of TelephonyManager through the following code.
Java code:
Copy the codeThe code is as follows:

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

In the Android platform, PhoneStateListener is a very useful listener, used to monitor the status of a phone, such as call status and connection services. Android monitor calls as follows:
Java code:
Copy the codeThe code 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 listen for the call status. There are three states defined in TelephonyManager, namely RINGING, OFFHOOK and Idle (IDLE). We can tell the current phone status through the value of the state.
After obtaining the TelephonyManager interface, call the listen() method to realize Android listening calls.
Java code:
(new TeleListener(),  PhoneStateListener.LISTEN_CALL_STATE);
Here is a simple test example, just appending the call status to the TextView.
Java code:
Copy the codeThe code is as follows:

package ;
import ; 
import ; 
import ;
import ;
import ; 
import ; 
import ;
public class Telephony extends Activity { 
private static final String TAG = "Telephony"; 
TextView view = null; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
(savedInstanceState); 
TelephonyManager mTelephonyMgr = (TelephonyManager) this  .getSystemService(Context.TELEPHONY_SERVICE); 
(new TeleListener(),  PhoneStateListener.LISTEN_CALL_STATE);
view = new TextView(this);
("listen the state of phone\n"); 
setContentView(view); 

class TeleListener extends PhoneStateListener {
@Override 
public void onCallStateChanged(int state, String incomingNumber) { 
(state, incomingNumber); 
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: {
(TAG, "CALL_STATE_IDLE"); 
("CALL_STATE_IDLE " + "\n"); 
break; 

case TelephonyManager.CALL_STATE_OFFHOOK: {
(TAG, "CALL_STATE_OFFHOOK"); 
("CALL_STATE_OFFHOOK" + "\n"); 
break; 

case TelephonyManager.CALL_STATE_RINGING: { 
(TAG, "CALL_STATE_RINGING"); 
("CALL_STATE_RINGING" + "\n"); 
break; 

default:  break; 

}
}
}

Don't forget to add a permission.
Java code:
< uses-permission android:name=".READ_PHONE_STATE" />