1

我已截获来电并在标准屏幕上显示我的活动。在此活动中,我有“接听电话”和“拒绝电话”按钮,但我无法正常工作。

我找到了两种以编程方式接听/拒绝电话的解决方案:

  1. 使用 ITelephony.aidl 但它仅在 api v10 之前有效。所以这是错误的方式
  2. 有以下内容:

    private void acceptCall(Context context) {
      Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
      buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
            KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
      context.sendOrderedBroadcast(buttonDown,
            "android.permission.CALL_PRIVILEGED");
        // froyo and beyond trigger on buttonUp instead of buttonDown
      Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
      buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
            KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
      context.sendOrderedBroadcast(buttonUp,
            "android.permission.CALL_PRIVILEGED");
    }
    
    
    private void acceptCall(Context context) {
      Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
      buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
            KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
      context.sendOrderedBroadcast(buttonDown,
            "android.permission.CALL_PRIVILEGED");
      // froyo and beyond trigger on buttonUp instead of buttonDown
      Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
      buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
            KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
      context.sendOrderedBroadcast(buttonUp,
            "android.permission.CALL_PRIVILEGED");
    }
    

从这里http://www.codeproject.com/Tips/578817/Reject-and-Accept-an-Incoming-Call

然而什么都没有!我单击按钮并相应地调用上面的方法,但没有任何反应。我在几个设备上测试了它,结果是一样的。

嗯,然后我在 samsung nexus one 上回答成功,但在 HTC 上回答不了。拒绝功能对两者都不起作用。

4

4 回答 4

2

结果我发现了http://androidbridge.blogspot.com/2011/05/how-to-answer-incoming-call-in-android.html

这是适用于 HTC 设备的解决方案:

在 Android 2.3.3 HTC Sensation 中,这段代码不起作用。原因是在 2.3.3 中我发现一个 HeadsetObserver 正在监听实际的蓝牙插件事件。所以你需要发送一个 Intent 假装已经连接了一个耳机。要解决此问题,您需要在调用上述代码之前发送 ACTION_HEADSET_PLUG Intent。

所以我在接受调用代码之前添加了以下代码:

Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
 headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
 headSetUnPluggedintent.putExtra("state", 1); // 0 = unplugged  1 = Headset with microphone 2 = Headset without microphone
 headSetUnPluggedintent.putExtra("name", "Headset");
 // TODO: Should we require a permission?
 sendOrderedBroadcast(headSetUnPluggedintent, null);

所以最终的工作代码是

    private void acceptCall() {
    setHeadSetConnectEmulated();
    Intent buttonUP = new Intent(Intent.ACTION_MEDIA_BUTTON);
    buttonUP.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
    IncomingCallActivity.this.getApplicationContext().sendOrderedBroadcast(buttonUP, "android.permission.CALL_PRIVILEGED");
}

private void setHeadSetConnectEmulated(){
    Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
    headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
    headSetUnPluggedintent.putExtra("state", 1); // 0 = unplugged  1 = Headset with microphone 2 = Headset without microphone
    headSetUnPluggedintent.putExtra("name", "Headset");
    // TODO: Should we require a permission?
    sendOrderedBroadcast(headSetUnPluggedintent, null);
}

private void rejectCall() {
    Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
    buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
    IncomingCallActivity.this.getApplicationContext().sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");
}
于 2013-11-04T12:00:58.013 回答
1

我有同样的问题。按钮向上/向下意图的目的是模仿耳机的回答。如果没有插入耳机,某些手机和android版本会忽略intent。您需要先检查耳机状态(以便可以在事后重置)并发送intent以“插入”耳机。

于 2013-10-31T15:58:39.027 回答
0

除了我试图从我创建的应用程序拨打电话外,我遇到了类似的问题。通过将此添加到清单中进行了补救。

 <uses-permission android:name="android.permission.CALL_PHONE" >
</uses-permission>
于 2013-10-31T15:14:07.513 回答
0

您可以使用此 Reciver 来取件或拒绝,

public class AutoAnswerIntentService extends BroadcastReceiver {

Context context = null;
private static String mFileName = null;

private static final String TAG = "in reciver";

@Override
public void onReceive(Context context, Intent intent) {
    // Toast.makeText(context, "calling now", Toast.LENGTH_LONG).show();
    if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
        return;
    else {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        String number = intent
                .getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.d(TAG, "Incoming call from: " + number);
        Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
        buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
                KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
        try {
            context.sendOrderedBroadcast(buttonUp,
                    "android.permission.CALL_PRIVILEGED");
            Log.d(TAG, "ACTION_MEDIA_BUTTON broadcasted...");
            // startRecording();
        } catch (Exception e) {
            Log.d(TAG, "Catch block of ACTION_MEDIA_BUTTON broadcast !");


            return;
        } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

            Log.d(TAG, "CALL ANSWERED NOW !!");
            return;
        } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            Log.d(TAG, "ALL DONE IN ELSE IF...... !!");

        } else {
            Log.d(TAG, "ALL DONE IN ELSE ...... !!");

        }
    }
}

并在清单文件中添加此权限

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
于 2013-10-31T16:03:16.663 回答