To realize the monitoring of mobile phone status, it mainly relies on two categories: TelephoneManger and PhoneStateListener.
TelephonseManger provides a way to obtain information on basic mobile phone services. Therefore, the application can use TelephonyManager to detect the basic service of the mobile phone. The application can register a listener to monitor changes in the phone's status. We cannot instantiate TelephonyManager, we can only obtain services:
(Context.TELEPHONY_SERVICE);
Note: Reading certain information on your mobile phone requires a certain permission.
Main static member constants: (They correspond to what PhoneStateListener.LISTEN_CALL_STATE listens)
int CALL_STATE_IDLE Idle state, no activity.
int CALL_STATE_OFFHOOK Off-hook status, at least one phone activity. The event is either dialing or calling, or on hold. And no phone is ringing or waiting
int CALL_STATE_RINGING The call status, the time when the phone rings, or the time when the call is on the phone, the time when the new call has to wait.
The corresponding value of mobile phone call status in broadcast
EXTRA_STATE_IDLE It is used to represent the CALL_STATE_IDLE status in broadcasts where the mobile phone call status changes.
EXTRA_STATE_OFFHOOK It is used to represent the CALL_STATE_OFFHOOK status in broadcasts where the mobile phone call status changes.
EXTRA_STATE_RINGING It is used to represent the CALL_STATE_RINGING state in broadcasts where the mobile phone call status changes.
ACTION_PHONE_STATE_CHANGED Use ACTION_PHONE_STATE_CHANGED to indicate the broadcast of the call status change (intent).
Note: License READ_PHONE_STATE is required.
String EXTRA_INCOMING_NUMBER
The broadcast of the phone call status changes, is used to retrieve incoming call numbers from extra.
String EXTRA_STATE broadcast in a call state changed, used to retrieve call state from extra.
Main member functions
public int getCallState() Gets the call status of the phone.
public CellLocation getCellLocation () Returns the current location of the phone. If the current location service is not available, return null
Note: Permission is required ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.
public int getDataActivity () Returns the current data connection activity status.
public int getDataState () Returns the current data connection status.
public String getDeviceId ()
Returns the device ID of the phone. For example, for GSM mobile phones, it is an IMEI code, and for CDMA mobile phones, it is an MEID code or ESN code. If the read fails, null is returned.
How to monitor the phone status?
When Android changes the phone status, it will send a broadcast with the action of .PHONE_STATE, and when making a call, it will send a broadcast with the action of .NEW_OUTGOING_CALL. However, I read the development document and did not find any broadcast on incoming calls. With a custom broadcast receiver, you can accept the above two broadcasts.
Java code:
package ;
import ;
import ;
import ;
import ;
import ;
import ;
public class PhoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
("action"+());
//If it's a call
if(().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
String phoneNumber = intent
.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
(TAG, "call OUT:" + phoneNumber);
}else{
//I checked the Android document and it seems that there is no action specifically used to receive incoming calls, so it is either a call or an incoming call.
//If we want to monitor the dialing status of the phone, we need a few steps:
* First: Obtain TelephonyManager manager = (TELEPHONY_SERVICE);
* Second: Register the phone status change event we want to listen to through TelephonyManager. (new MyPhoneStateListener(),
* PhoneStateListener.LISTEN_CALL_STATE); Here PhoneStateListener.LISTEN_CALL_STATE is what we want
* The listening status changes event, and there are many other events besides the first time.
* Step 3: Customize your own rules through extends PhoneStateListener. Pass its object to the second step as a parameter.
* Step 4: This step is very important, that is, add permissions to the application. .READ_PHONE_STATE
TelephonyManager tm = (TelephonyManager)(Service.TELEPHONY_SERVICE);
(listener, PhoneStateListener.LISTEN_CALL_STATE);
//Set a listener
}
}
PhoneStateListener listener=new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
//Note that the method must be written after the super method, otherwise the incomingNumber cannot get the value.
(state, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_IDLE:
("hang up");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
("Ack");
break;
case TelephonyManager.CALL_STATE_RINGING:
("Ring: incoming number"+incomingNumber);
//Output call number
break;
}
}
};
}
To register a broadcast receiver:
<receiver android:name=".PhoneReceiver">
<intent-filter>
<action android:name=".PHONE_STATE"/>
<action android:name=".NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<receiver android:name=".PhoneReceiver"> <intent-filter> <action android:name=".PHONE_STATE"/> <action android:name=".NEW_OUTGOING_CALL" /> </intent-filter> </receiver>
Also add permissions:
<uses-permission android:name=".READ_PHONE_STATE"></uses-permission>
<uses-permission android:name=".PROCESS_OUTGOING_CALLS"></uses-permission>
TelephonseManger provides a way to obtain information on basic mobile phone services. Therefore, the application can use TelephonyManager to detect the basic service of the mobile phone. The application can register a listener to monitor changes in the phone's status. We cannot instantiate TelephonyManager, we can only obtain services:
(Context.TELEPHONY_SERVICE);
Note: Reading certain information on your mobile phone requires a certain permission.
Main static member constants: (They correspond to what PhoneStateListener.LISTEN_CALL_STATE listens)
int CALL_STATE_IDLE Idle state, no activity.
int CALL_STATE_OFFHOOK Off-hook status, at least one phone activity. The event is either dialing or calling, or on hold. And no phone is ringing or waiting
int CALL_STATE_RINGING The call status, the time when the phone rings, or the time when the call is on the phone, the time when the new call has to wait.
The corresponding value of mobile phone call status in broadcast
EXTRA_STATE_IDLE It is used to represent the CALL_STATE_IDLE status in broadcasts where the mobile phone call status changes.
EXTRA_STATE_OFFHOOK It is used to represent the CALL_STATE_OFFHOOK status in broadcasts where the mobile phone call status changes.
EXTRA_STATE_RINGING It is used to represent the CALL_STATE_RINGING state in broadcasts where the mobile phone call status changes.
ACTION_PHONE_STATE_CHANGED Use ACTION_PHONE_STATE_CHANGED to indicate the broadcast of the call status change (intent).
Note: License READ_PHONE_STATE is required.
String EXTRA_INCOMING_NUMBER
The broadcast of the phone call status changes, is used to retrieve incoming call numbers from extra.
String EXTRA_STATE broadcast in a call state changed, used to retrieve call state from extra.
Main member functions
public int getCallState() Gets the call status of the phone.
public CellLocation getCellLocation () Returns the current location of the phone. If the current location service is not available, return null
Note: Permission is required ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.
public int getDataActivity () Returns the current data connection activity status.
public int getDataState () Returns the current data connection status.
public String getDeviceId ()
Returns the device ID of the phone. For example, for GSM mobile phones, it is an IMEI code, and for CDMA mobile phones, it is an MEID code or ESN code. If the read fails, null is returned.
How to monitor the phone status?
When Android changes the phone status, it will send a broadcast with the action of .PHONE_STATE, and when making a call, it will send a broadcast with the action of .NEW_OUTGOING_CALL. However, I read the development document and did not find any broadcast on incoming calls. With a custom broadcast receiver, you can accept the above two broadcasts.
Java code:
Copy the codeThe code is as follows:
package ;
import ;
import ;
import ;
import ;
import ;
import ;
public class PhoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
("action"+());
//If it's a call
if(().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
String phoneNumber = intent
.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
(TAG, "call OUT:" + phoneNumber);
}else{
//I checked the Android document and it seems that there is no action specifically used to receive incoming calls, so it is either a call or an incoming call.
//If we want to monitor the dialing status of the phone, we need a few steps:
* First: Obtain TelephonyManager manager = (TELEPHONY_SERVICE);
* Second: Register the phone status change event we want to listen to through TelephonyManager. (new MyPhoneStateListener(),
* PhoneStateListener.LISTEN_CALL_STATE); Here PhoneStateListener.LISTEN_CALL_STATE is what we want
* The listening status changes event, and there are many other events besides the first time.
* Step 3: Customize your own rules through extends PhoneStateListener. Pass its object to the second step as a parameter.
* Step 4: This step is very important, that is, add permissions to the application. .READ_PHONE_STATE
TelephonyManager tm = (TelephonyManager)(Service.TELEPHONY_SERVICE);
(listener, PhoneStateListener.LISTEN_CALL_STATE);
//Set a listener
}
}
PhoneStateListener listener=new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
//Note that the method must be written after the super method, otherwise the incomingNumber cannot get the value.
(state, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_IDLE:
("hang up");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
("Ack");
break;
case TelephonyManager.CALL_STATE_RINGING:
("Ring: incoming number"+incomingNumber);
//Output call number
break;
}
}
};
}
To register a broadcast receiver:
Copy the codeThe code is as follows:
<receiver android:name=".PhoneReceiver">
<intent-filter>
<action android:name=".PHONE_STATE"/>
<action android:name=".NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<receiver android:name=".PhoneReceiver"> <intent-filter> <action android:name=".PHONE_STATE"/> <action android:name=".NEW_OUTGOING_CALL" /> </intent-filter> </receiver>
Also add permissions:
Copy the codeThe code is as follows:
<uses-permission android:name=".READ_PHONE_STATE"></uses-permission>
<uses-permission android:name=".PROCESS_OUTGOING_CALLS"></uses-permission>