我想用我的程序拨打电话,然后识别通话状态。我想检测传出铃声的结束。
如何检测传出铃声的结束?
我使用Flowing代码进行make调用。
EditText ed = (EditText) findViewById(R.id.txtcall);
Uri uri = Uri.fromParts("tel", ed.getText().toString(), null);
callIntent = new Intent(Intent.ACTION_CALL,uri);
startActivity(callIntent);
答案 0 :(得分:1)
以下代码段可以帮助您找到电话当前雕像,无论是拾取,响铃还是闲置。您可以轻松使用这些状态并相应地实现您的功能。
private class CustomPhoneStateListener extends PhoneStateListener
{
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
//TelephonyManager.CALL_STATE_IDLE
//TelephonyManager.CALL_STATE_OFFHOOK
//TelephonyManager.CALL_STATE_RINGING
}
}
答案 1 :(得分:0)
通过以下方式检查: - 您将能够呼叫用户
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+9189745847"));
startActivity(callIntent);
并且还在androidmanifest.xml文件中使用:
<uses-permission android:name="android.permission.CALL_PHONE" />
答案 2 :(得分:0)
查看意图NEW_OUTGOING_CALL
<receiver android:name=".receiver.OutgoingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
然后您可以使用PhoneStateListener检查调用状态:
public class OutgoingCallReceiver extends BroadcastReceiver {
protected static final String CLASS_TAG = "OutgoingCallReceiver : ";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle) return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(LOG_TAG, CLASS_TAG + "phone number " + phonenumber);
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d(LOG_TAG, CLASS_TAG + "RINGING");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(LOG_TAG, CLASS_TAG + "OFFHOOK");
Process.onCallStateIsOFFHOOK();
break;
case TelephonyManager.CALL_STATE_IDLE:
Process.onCallStateIsIDLE();
Log.d(LOG_TAG, CLASS_TAG + "IDLE");
break;
default:
Log.d(LOG_TAG, CLASS_TAG + "Default: " + state);
break;
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}
}
在Process类中,您可以通过录制VOICE_CALL来检测铃声并创建更多状态,如CALLBEGINS,CALLANSWERED。 onCallStateIsIDLE()的示例:
public void onCallStateIsIDLE() {
setSpeakingOn(false);
if (stateCall == STATE_CALLANSWERED ||
stateCall == STATE_CALLBEGINS ||
stateCall == STATE_DIALISOFFHOOK ||
stateCall == STATE_DIALENDREQUESTED) {
Log.i(LOG_TAG, CLASS_TAG + " - forcing state : STATE_DIALREADY : old state = " + stateCall);
stateCall = STATE_DIALREADY;
}
}