gpt4 book ai didi

android - 跟踪来电操作

转载 作者:行者123 更新时间:2023-11-30 04:11:59 25 4
gpt4 key购买 nike

我知道这是一个被问及的问题。通过使用广播接收器并使用android.intent.action.PHONE_STATE

在receiver标签中,可以知道手机的 Action 。但是如何判断是来电还是去电呢?

这是我的代码

@Override
public void onReceive(Context context, Intent intent)
{
this.context = context ;
System.out.println(":::called onReceiver:::");
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private final PhoneStateListener phoneCallListener = new PhoneStateListener()
{
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
switch(state)
{
case TelephonyManager.CALL_STATE_RINGING:
isRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (isRinging)
{
hasAttended = true ;
isRinging = false;
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (hasAttended)
{
isCallEnded = true ;
hasAttended = false;
}
break;
}
if(isCallEnded)
{
isCallEnded=false;
Intent callIntent = new Intent(context.getApplicationContext(),MyActivity.class);
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);
}
super.onCallStateChanged(state, incomingNumber);
}
};

这里每次创建一个phoneCallListener对象,每次增加onCallStateChanged的调用率..

最佳答案

This是一个方便的教程,它同时使用广播接收器和使用 android.intent.action.PHONE_STATE 创建不同的 toast,以根据调用状态显示。当你得到这个工作时,你将能够操纵代码在传入或传出调用时执行你想要的操作

首先你的类必须extend BroadcastReceiver

接下来你必须创建一个方法来监听手机状态... PhoneStateListener 当手机状态改变时它会监听

然后做一个简单的switch case来根据是来电还是去电来接听电话

编辑

您需要做的就是编写一些代码来检查之前的状态是否为“响铃”。如果当前状态为空闲,而之前的状态为振铃,则他们取消/错过了调用。如果当前状态是摘机而之前的状态是振铃,则他们接听电话。

switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.i(LOG_TAG, "RINGING");
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(LOG_TAG, "OFFHOOK");

if (!wasRinging) {
// Start your new activity
} else {
// Cancel your old activity
}

// this should be the last piece of code before the break
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i(LOG_TAG, "IDLE");
// this should be the last piece of code before the break
wasRinging = true;
break;
}

关于android - 跟踪来电操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10719006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com