通话状态监听

平台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 {
    
    /** Call state is not valid (Not received a call state). */  //呼叫状态无效(未收到呼叫状态)
    public static final int PRECISE_CALL_STATE_NOT_VALID =      -1;
    /** Call state: No activity. */   //通话空闲
    public static final int PRECISE_CALL_STATE_IDLE =           0;
    /** Call state: Active. */       //正在通话(活动中)
    public static final int PRECISE_CALL_STATE_ACTIVE =         1;
    /** Call state: On hold. */     //通话挂起(例如我和多个人通话,其中一个通话在活动,而其它通话就会进入挂起状态)
    public static final int PRECISE_CALL_STATE_HOLDING =        2;
    /** Call state: Dialing. */  //拨号开始
    public static final int PRECISE_CALL_STATE_DIALING =        3;
    /** Call state: Alerting. */   //正在呼出(提醒对方接电话)
    public static final int PRECISE_CALL_STATE_ALERTING =       4;
    /** Call state: Incoming. */  //对方来电
    public static final int PRECISE_CALL_STATE_INCOMING =       5;
    /** Call state: Waiting. */   //第三方来电等待(例如我正在和某人通话,而其他人打入时就会就进入等待状态)
    public static final int PRECISE_CALL_STATE_WAITING =        6;
    /** Call state: Disconnected. */  //挂断完成
    public static final int PRECISE_CALL_STATE_DISCONNECTED =   7;
    /** Call state: Disconnecting. */  //正在挂断
    public static final int PRECISE_CALL_STATE_DISCONNECTING =  8;

PhoneStateListener.java

/**
 * Listen for changes to the device call state.
 * {@more}
 *
 * @see #onCallStateChanged
 */
public static final int LISTEN_CALL_STATE                               = 0x00000020;
 
     /**
 * Listen for {@link android.telephony.Annotation.PreciseCallStates} of ringing,
 * background and foreground calls.
 *
 * <p>Requires permission {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE}
 * or the calling app has carrier privileges
 * (see {@link TelephonyManager#hasCarrierPrivileges}).
 *
 * @hide
 */
@RequiresPermission((android.Manifest.permission.READ_PRECISE_PHONE_STATE))
@SystemApi
public static final int LISTEN_PRECISE_CALL_STATE                       = 0x00000800;
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值