简体   繁体   English

你如何接收broadcastreceiver的拨出电话

[英]How do you receive outgoing call in broadcastreceiver

I am trying to identify and transfer to an activity after an outgoing call is initiated. 我正在尝试在启动拨出呼叫后识别并转移到活动。 I used ACTION_NEW_OUTGOING_CALL in the Intent filter. 我在Intent过滤器中使用了ACTION_NEW_OUTGOING_CALL However how csn I identify that the call is outgoing. 但是我怎么认识到呼叫是外向的。 I did this for an incoming call (as seen below) but what can I use instead of EXTRA_STATE_RINGING . 我为来电做了这个(如下所示),但我可以使用什么而不是EXTRA_STATE_RINGING

public class OutgoingBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
        {
            Intent i = new Intent(context, OutgoingCallScreenDisplay.class);
            i.putExtras(intent);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}

在此输入图像描述

public class OutgoingBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
           // If it is to call (outgoing)
           Intent i = new Intent(context, OutgoingCallScreenDisplay.class);
           i.putExtras(intent);
           i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           context.startActivity(i);
        }
    }
}

ACTION_NEW_OUTGOING_CALL is a constant declare in the Intent class, not in TelephonyManager . ACTION_NEW_OUTGOING_CALLIntent类中的常量声明,而不是TelephonyManager的常量声明。 When an outgoing call appears, then the system broadcasts an intent with this constant. 当出现呼出呼叫时,系统使用此常量广播意图。 If you really want to catch an outgoing call by using TelephonyManager then do this: 如果您真的想通过使用TelephonyManager捕获拨出电话,请执行以下操作:

TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);   
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
PhoneStateListener listener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        // TODO Auto-generated method stub

        super.onCallStateChanged(state, incomingNumber);
        switch(state) {
            case TelephonyManager.CALL_STATE_IDLE:
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                if(incomingNumber==null) {
                    //outgoing call
                } else {
                    //incoming call
                }
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                if(incomingNumber==null) {
                    //outgoing call
                } else {
                    //incoming call
                }
                break;
        }
    }
};

Detect outgoing phone call event 检测拨出电话事件

1. Create OutgoingCallBroadcastReceiver 1.创建OutgoingCallBroadcastReceiver

public class OutgoingCallReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(OutgoingCallReceiver.class.getSimpleName(), intent.toString());
        Toast.makeText(context, "Outgoing call catched!", Toast.LENGTH_LONG).show();
        //TODO: Handle outgoing call event here
    }
}

2. Register OutgoingCallBroadcastReceiver in AndroidManifest.xml 2.在AndroidManifest.xml中注册OutgoingCallBroadcastReceiver

<receiver android:name=".OutgoingCallReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    </intent-filter>
</receiver>

3. Add permission in AndroidManifest.xml 3.在AndroidManifest.xml中添加权限

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

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

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