Android 如何获取传出呼叫的状态
在Android 如何获取传出呼叫的状态,android,broadcastreceiver,Android,Broadcastreceiver,在OnReceive方法中,我有如下内容: Bundle bundle=intent.getExtras(); String phonenumber=intent.getStrngExtra(Intent.EXTRA_PHONE_NUMBER); <receiver android:name=".CallDurationReceiver"> <intent-filter> <action android:name=
OnReceive
方法中,我有如下内容:
Bundle bundle=intent.getExtras();
String phonenumber=intent.getStrngExtra(Intent.EXTRA_PHONE_NUMBER);
<receiver android:name=".CallDurationReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
}
如何检查拨号电话是否仍然接通或客户是否挂断了电话?
如何检查电话是否接听
当客户端挂断电话或被叫客户端接听电话时,我需要打印一个toat。您需要为actionandroid.intent.action.PHONE\u STATE注册一个广播接收器。如果手机状态在挂断后变为空闲,则表示通话仍在继续。 如果read phone state broadcast receiver中的状态更改为offhook,则呼叫已接听。在这些州,根据需要敬酒
public class CallDurationReceiver extends BroadcastReceiver {
static boolean flag =false;
static long start_time,end_time;
@Override
public void onReceive(Context arg0, Intent intent) {
String action = intent.getAction();
if(action.equalsIgnoreCase("android.intent.action.PHONE_STATE")){
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
//tOAST FOR INCOMING CALL, NOT YET PICKED UP
}
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)) {
end_time=System.currentTimeMillis();
//Total time talked =
long total_time = end_time-start_time;
//Store total_time somewhere or pass it to an Activity using intent
} if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK)) {
start_time=System.currentTimeMillis();
}
}
}
在清单文件中注册收件人,如下所示:
Bundle bundle=intent.getExtras();
String phonenumber=intent.getStrngExtra(Intent.EXTRA_PHONE_NUMBER);
<receiver android:name=".CallDurationReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
}
}
同时添加使用权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
您可以使用以下代码来处理呼叫状态:
private Runnable callMonitor = new Runnable() {
public void run() {
try {
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)m_activity.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch(Exception e) {
Log.e("callMonitor", "Exception: "+e.toString());
}
}
};
private class EndCallListener extends PhoneStateListener {
private boolean active = false;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i("EndCallListener", "RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
//wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
active = true;
Log.i("EndCallListener", "OFFHOOK");
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
//when this state occurs, and your flag is set, restart your app
Log.i("EndCallListener", "IDLE");
if (active) {
active = false;
// stop listening
TelephonyManager mTM = (TelephonyManager)m_activity.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(this, PhoneStateListener.LISTEN_NONE);
// restart the inbox activity
// Intent intent = new Intent(m_activity, MDInboxActivity.class);
// m_activity.startActivity(intent);
}
}
}
}
我找到了一个解决方案,并成功地实现了它。不可能获取被叫方接受传出呼叫的确切时间 在另一端接听电话之前,它已经经历了两个阶段,即
on\u State\u idle
和on\u State\u offhook
On_state_响铃
对拨出的电话不起作用
让我们假设,如果对方没有接听电话,电话会连续响40秒(我不确定)
on_State\u idle
和on_State\u offhook
on_State\u idle->on_State\u offhook->on_State\u idle
在40秒内工作,则表示另一方没有接听我的电话状态定义的含义是什么?只需在活动中粘贴这两个函数,然后再拨打任何号码。如何检查传出呼叫是否已取消?拨打时立即调用call_state_OFFHOOK。没有可用于确定接收器是否已拿起电话的状态。这不是答案。它不适用于知道何时应答传出消息。只有当电话在另一端响起时,我才需要检测呼出的电话何时被取消或挂断。如何检查?你不能区分呼入和呼出。我给了你上面的代码。当状态为空闲时,呼叫结束。因此,如果条件允许,请将所有代码置于空闲状态。此链接访问此链接使用sip端点到移动enpoint进行呼叫时,获取呼叫持续时间如何?这里的通话记录没有通话持续时间。