平台android11
AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PRECISE_PHONE_STATE"/>
<receiver android:name=".CallStateReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
CallStateReceiver.java
import android.telephony.PreciseCallState;
import android.telephony.TelephonyManager;
import android.app.Service;
import android.telephony.PhoneStateListener;
public class CallStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
PhoneStateListener listener = new PhoneStateListener(){
@Override
public void onPreciseCallStateChanged(PreciseCallState preciseState) {
Log.d(TAG, "onPreciseCallStateChanged: " + preciseState.toString());
if(preciseState.getForegroundCallState() == PreciseCallState.PRECISE_CALL_STATE_ACTIVE) {
}
}
};
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE
| PhoneStateListener.LISTEN_PRECISE_CALL_STATE);
}
}
PreciseCallState 十种状态详解
public final class PreciseCallState implements Parcelable {
public static final int PRECISE_CALL_STATE_NOT_VALID = -1;
public static final int PRECISE_CALL_STATE_IDLE = 0;
public static final int PRECISE_CALL_STATE_ACTIVE = 1;
public static final int PRECISE_CALL_STATE_HOLDING = 2;
public static final int PRECISE_CALL_STATE_DIALING = 3;
public static final int PRECISE_CALL_STATE_ALERTING = 4;
public static final int PRECISE_CALL_STATE_INCOMING = 5;
public static final int PRECISE_CALL_STATE_WAITING = 6;
public static final int PRECISE_CALL_STATE_DISCONNECTED = 7;
public static final int PRECISE_CALL_STATE_DISCONNECTING = 8;
PhoneStateListener.java
public static final int LISTEN_CALL_STATE = 0x00000020;
@RequiresPermission((android.Manifest.permission.READ_PRECISE_PHONE_STATE))
@SystemApi
public static final int LISTEN_PRECISE_CALL_STATE = 0x00000800;
|