在我的应用程序中,我正在调用一些电话号码。有什么方法我可以检查是否有呼叫蜂鸣声,并建立连接以确保电话号码和结束呼叫的有效性,然后再接听。
答案 0 :(得分:0)
1)首先,您必须在清单文件中添加以下权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
2)然后你必须定义一个广播接收器
<receiver android:name=".AnswerCallBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
然后在广播接收器中添加以下代码
public class AnswerCallBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
if(arg1.getAction().equals("android.intent.action.PHONE_STATE")){
String state = arg1.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
Log.d(TAG, "Inside Extra state off hook");
String number = arg1.getStringExtra(TelephonyManager.EXTRA_PHONE_NUMBER);
Log.e(TAG, "outgoing number : " + number);
}
else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
Log.e(TAG, "Inside EXTRA_STATE_RINGING");
String number = arg1.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.e(TAG, "incoming number : " + number);
}
else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
Log.d(TAG, "Inside EXTRA_STATE_IDLE");
}
}
}
}
答案 1 :(得分:0)
我会尝试猜测你的代码,因为你没有给我任何线索:(
为了获得电话呼叫状态,您应该向Telephony Manager添加一个监听器。
创建一个监听器类:
// Or just make new PhoneStateListener() and override onCallStateChanged
public class PhoneCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
//A new call arrived and is ringing or waiting. In the latter case, another call is already active
}
if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
//At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting
}
if (state == TelephonyManager.CALL_STATE_IDLE) {
//No activity
}
}
}
添加监听器:
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
在您收听电话呼叫状态后,您应该以这样的方式启动电话:
Intent callIntent = new Intent(Intent.ACTION_CALL);
CallInfo callInfo = new CallInfo(PHONE_NUMBER);
callIntent.setData(Uri.parse("tel:" + callInfo.getDialTo()));
//** Check for permision **//
startActivity(callIntent);
为了使这种情况正确发生,您必须获得执行呼叫和侦听电话呼叫状态的权限,因此请将以下权限添加到manifest.xml:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />