简体   繁体   English

android接听来电

[英]android answer incoming call

I have intercepted incoming call and show my activity over standard screen. 我拦截了来电并在标准屏幕上显示我的活动。 On this activity I have buttons "answer call" and "reject call", but I can't do it working. 在这个活动中我有按钮“接听电话”和“拒绝电话”,但我不能这样做。

I have found two solutions to answer/reject phone call programaticaly: 我找到了两种解决方案来接听/拒绝电话号码:

  1. With ITelephony.aidl but it works only before api v10. 使用ITelephony.aidl但它只能在api v10之前运行。 So it is wrong way 所以这是错误的方式
  2. With following: 有以下几点:

     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"); } 

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

However nothing! 不过没什么! I click buttons and invoke methods above accordingly, and nothing happens. 我点击按钮并相应地调用上面的方法,没有任何反应。 I tested it on several devices, and result the same. 我在几台设备上进行了测试,结果相同。

Hmm, then I was successful with answering on samsung nexus one, but not on HTC with sense. 嗯,然后我成功地回答了三星nexus one,但没有回答HTC的感觉。 Reject function doesnt work on both. 拒绝功能对两者都不起作用。

As result I have found http://androidbridge.blogspot.com/2011/05/how-to-answer-incoming-call-in-android.html 结果我找到了http://androidbridge.blogspot.com/2011/05/how-to-answer-incoming-call-in-android.html

Here is solution which works on HTCs devices: 这是适用于HTC设备的解决方案:

In Android 2.3.3 HTC Sensation this piece of code does not work. 在Android 2.3.3 HTC Sensation中,这段代码不起作用。 Reason is in 2.3.3 I found a HeadsetObserver listening for actual bluetooth plug-in event. 原因是在2.3.3我发现HeadsetObserver正在侦听实际的蓝牙插件事件。 So you need to send a Intent pretending there is a headset connected already. 因此,您需要发送一个Intent假装已经连接了耳机。 To fix this problem you need to send the ACTION_HEADSET_PLUG Intent before calling the above code. 要解决此问题,您需要在调用上述代码之前发送ACTION_HEADSET_PLUG Intent。

So I added following code before accept call code: 所以我在接受调用代码之前添加了以下代码:

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);

so final worked code is 所以最终工作的代码是

    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");
}

I have had the same issue. 我有同样的问题。 The purpose of the button up/down intents is to mimic a headset answer. 按钮向上/向下意图的目的是模仿耳机应答。 Some phones and versions of android will ignore the intents if there is not headset plugged in. You need to first check the headset state (so it can be reset afterwords) and send an intent to "plug in" a headset. 如果没有插入耳机,某些手机和Android版本将忽略意图。您需要先检查耳机状态(以便可以重置词后)并发送“插入”耳机的意图。

I had a similar problem except I was trying to make a call from an app I created. 除了我试图通过我创建的应用程序拨打电话之外,我遇到了类似的问题。 It was remedied by adding this to the manifest. 通过将此添加到清单来解决问题。

 <uses-permission android:name="android.permission.CALL_PHONE" >
</uses-permission>

you can use this Reciver To Pickup or reject, 您可以使用此Reciver To Pickup或拒绝,

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 ...... !!");

        }
    }
}

} and in manifest file add this permission 并在清单文件中添加此权限

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM