android phone status monitoring of incoming and outgoing calls implementation code

  • 2020-05-07 20:26:00
  • OfStack

Monitoring the status of mobile phone calls mainly depends on two classes: TelephoneManger and PhoneStateListener.
TelephonseManger provides one way to get information about basic mobile services. So the application can use TelephonyManager to probe the phone's basic services. Applications can register listener to listen for changes in phone status. We cannot instantiate TelephonyManager, only by obtaining the form of a service:
Context.getSystemService(Context.TELEPHONY_SERVICE);
Note: certain information on the phone can be read with an permission license.
Main static member constants :(they correspond to what PhoneStateListener.LISTEN_CALL_STATE is listening for)
int CALL_STATE_IDLE idle state with no activity.
int CALL_STATE_OFFHOOK offline status with at least one call active. The activity is either by dialing (dialing) or calling, or on hold. And no phone number is ringing or waiting
int CALL_STATE_RINGING caller status: the period during which the phone rings or is in the middle of a call and has to wait.
The corresponding value of the mobile phone call status in the broadcast
EXTRA_STATE_IDLE it is used to indicate the CALL_STATE_IDLE state in the broadcast of a mobile phone call state change
EXTRA_STATE_OFFHOOK it is used to indicate the CALL_STATE_OFFHOOK state in the broadcast of a mobile phone call state change
EXTRA_STATE_RINGING it is used to indicate the CALL_STATE_RINGING status in the broadcast of a mobile phone call status change
ACTION_PHONE_STATE_CHANGED USES the ACTION_PHONE_STATE_CHANGED in the broadcast to mark the broadcast of the call status change (intent).
Note: permission is required for READ_PHONE_STATE.
String EXTRA_INCOMING_NUMBER
The broadcast in the mobile phone call state changes, used to fetch the calling number from extra.
String EXTRA_STATE broadcast when the call state changes, used to fetch the call state from extra.

main member function
public int getCallState() gets the phone's call status.
public CellLocation getCellLocation () returns the current location of the phone. If the current location service is not available, null is returned
Note: license (Permission) ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION is required.
public int getDataActivity () returns the status of the current data connection activity.
public int getDataState () returns the status of the current data connection.
public String getDeviceId ()
Return to the phone's device ID. For example, IMEI code for GSM phone, MEID code or ESN code for CDMA phone. If the read fails, null is returned.

How does monitor the phone status?
Android state change the phone is will send action for android intent. action. PHONE_STATE broadcasting, and make a phone call when sending action for android. intent. action. NEW_OUTGOING_CALL radio, but I look at the document under development, temporarily haven't found the call when the radio. The above two broadcasts are accepted by a custom broadcast receiver.
Java code:
 
package com.pocketdigi.phonelistener; 
import android.app.Service; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
public class PhoneReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
System.out.println("action"+intent.getAction()); 
// If it's deelectrified  
if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){ 
String phoneNumber = intent 
.getStringExtra(Intent.EXTRA_PHONE_NUMBER); 
Log.d(TAG, "call OUT:" + phoneNumber); 
}else{ 
// Look up the android Documents that don't seem to be dedicated to receiving incoming calls action, Therefore, either to the electricity is incoming . 
// If we want to monitor phone calls, we need to take these steps  : 
*  The first 1 : gets the telephone service manager TelephonyManager manager = this.getSystemService(TELEPHONY_SERVICE); 
*  The first 2 Through: TelephonyManager Register the phone status change event we are listening for. manager.listen(new MyPhoneStateListener(), 
* PhoneStateListener.LISTEN_CALL_STATE); Here, PhoneStateListener.LISTEN_CALL_STATE That's what we want  
*  Listen for state change events, there are many other events besides the first one.  
*  The first 3 Step: extends PhoneStateListener To customize your own rules. Pass its object to the first 2 Step as parameter.  
*  The first 4 Step: 1 The important step is to add permissions to the application. android.permission.READ_PHONE_STATE 
TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE); 
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE); 
// Set up the 1 A listener  
} 
} 
PhoneStateListener listener=new PhoneStateListener(){ 
@Override 
public void onCallStateChanged(int state, String incomingNumber) { 
// Note that the method must be written in super After the method, otherwise incomingNumber Could not get the value.  
super.onCallStateChanged(state, incomingNumber); 
switch(state){ 
case TelephonyManager.CALL_STATE_IDLE: 
System.out.println(" Hang up "); 
break; 
case TelephonyManager.CALL_STATE_OFFHOOK: 
System.out.println(" answer "); 
break; 
case TelephonyManager.CALL_STATE_RINGING: 
System.out.println(" Ring the bell : Telephone number "+incomingNumber); 
// Outgoing call number  
break; 
} 
} 
}; 
} 

To register a broadcast receiver at AndroidManifest.xml:
 
<receiver android:name=".PhoneReceiver"> 
<intent-filter> 
<action android:name="android.intent.action.PHONE_STATE"/> 
<action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
</intent-filter> 
</receiver> 
<receiver android:name=".PhoneReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"/> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> </intent-filter> </receiver> 

Also add permissions:
 
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> 
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission> 

Related articles: