AndroidStudio-获取手机通话的PreciseCallState
创始人
2024-10-10 20:30:18
0

要获取手机通话的PreciseCallState,需要使用Telephony Manager和PhoneStateListener。首先,在AndroidManifest.xml文件中添加以下权限:


然后,在相应的Activity或Service中,创建Telephony Manager和PhoneStateListener对象并实现相应的回调函数:

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

PhoneStateListener phoneStateListener = new PhoneStateListener() {
  @Override
  public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);

    if (state == TelephonyManager.CALL_STATE_IDLE) {
      // no call
    } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
      // off hook
    } else if (state == TelephonyManager.CALL_STATE_RINGING) {
      // ringing
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
          PreciseCallState preciseCallState = telephonyManager.getPreciseCallState();
          // use preciseCallState object to get more information about the call state
      }
    }
  }
};

telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

上述代码中,我们使用TelephonyManager.getPreciseCallState()方法获取PreciseCallState对象。请注意,此方法需要在Android Q或更高版本中才能使用。

完整示例代码如下:

public class MainActivity extends AppCompatActivity {

    TelephonyManager telephonyManager;
    PhoneStateListener phoneStateListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

        phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);

                if (state == TelephonyManager.CALL_STATE_IDLE) {
                    Log.d("MainActivity", "no call");
                } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    Log.d("MainActivity", "off hook");
                } else if (state == TelephonyManager.CALL_STATE_RINGING) {
                    Log.d("MainActivity", "ringing");
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        PreciseCallState preciseCallState = telephonyManager.getPreciseCallState();
                        Log.d("MainActivity", "precise call state: " + preciseCallState);
                    }
                }
            }
        };

        telephonyManager

相关内容

热门资讯

1分钟辅助!wejoker黑侠... 1分钟辅助!wejoker黑侠辅助器,wepoker软件安装包,讲义教程(新版有挂)1、全新机制【w...
六分钟辅助!来玩德州破解器,s... 六分钟辅助!来玩德州破解器,sohoo poker辅助,步骤教程(有挂方法)1、操作简单,无需来玩德...
4分钟辅助!德州圈脚本,wep... 4分钟辅助!德州圈脚本,wepoker透视功能下载,学习教程(竟然有挂)1、操作简单,无需wepok...
6分钟辅助!wepoker插件... 6分钟辅助!wepoker插件程序激活码,wepoker透视脚本免费app,窍门教程(有挂解密)1、...
五分钟辅助!aapoker能控... 五分钟辅助!aapoker能控制牌吗,hhpoker透视脚本,学习教程(有挂方针)1、玩家可以在aa...
三分钟辅助!智星菠萝可以辅助吗... 三分钟辅助!智星菠萝可以辅助吗,德州真人透视脚本,绝活儿教程(有挂方法)三分钟辅助!智星菠萝可以辅助...
第8分钟辅助!wepoker有... 第8分钟辅助!wepoker有没有辅助,hhpoker必备开挂,法门教程(真实有挂)1、超多福利:超...
4分钟辅助!wejoker开挂... 4分钟辅助!wejoker开挂,sohoo poker辅助器,模板教程(有挂技巧)1、首先打开soh...
第七分钟辅助!wpk模拟器,w... 第七分钟辅助!wpk模拟器,wpk透视辅助靠谱吗,妙计教程(有挂细节)在进入wpk透视辅助靠谱吗软件...
第七分钟辅助!wepoker开... 第七分钟辅助!wepoker开辅助能查到吗,aapoker辅助器怎么用,经验教程(确实有挂)1、起透...