通话状态

发布于 2024-11-02 19:19:08 字数 105 浏览 7 评论 0原文

我们如何以编程方式知道我正在拨打的电话已在另一方收到? 我知道电话有一些状态,如空闲、摘机和振铃。 我希望收到通知:我正在拨打的电话已收到、对方已断开连接或对方无人接听。

请建议。

How do we know programmatically that the call I am making is received in the other side ?
I know that there are some phone states like IDLE, OFFHOOK and RINGING.
I want to be notified that the outgoing call I am making is received, is disconnected by the other side or is unattended by the other side.

Please suggest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自由如风 2024-11-09 19:19:08

尽管 Android 电话管理器中只有三种可用状态,但很容易识别电话呼叫中所有情况的状态。
所以这里我们收到了来自 android 的 3 个事件,例如 RINGING、OFFHOOK 和 IDLE。为了获取呼叫的等待状态,我们需要定义自己的状态,例如RINGING、OFFHOOK、IDLE、FIRST_CALL_RINGING、SECOND_CALL_RINGING
请以这样的方式思考:我们正在从 Android 接收事件,并且我们将定义我们的待命状态
以下是在广播接收器的 onReceive 方法中获取通话状态的方法,无需注册 phonestatelistener 并避免其他复杂情况。查看代码。

public class CallListening  extends BroadcastReceiver {
    static CustomPhoneStateListener customPhoneListener;
    private static final String TAG ="broadcast_intent";
    public static String incoming_number;
    private String current_state,previus_state,event;
    public static Boolean dialog= false;
    private Context context;
    private SharedPreferences sp,sp1;
    private SharedPreferences.Editor spEditor,spEditor1;
    public void onReceive(Context context, Intent intent) {
        //Log.d("intent_log", "Intent" + intent);
        dialog=true;
        this.context = context;
        event = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        incoming_number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.d(TAG, "The received event : "+event+", incoming_number : " + incoming_number);
        previus_state = getCallState(context);
        current_state = "IDLE";
        if(incoming_number!=null){
            updateIncomingNumber(incoming_number,context);
        }else {
            incoming_number=getIncomingNumber(context);
        }
        switch (event) {
            case "RINGING":
                Log.d(TAG, "State : Ringing, incoming_number : " + incoming_number);
            if((previus_state.equals("IDLE")) || (previus_state.equals("FIRST_CALL_RINGING"))){
                    current_state ="FIRST_CALL_RINGING";
                }
                if((previus_state.equals("OFFHOOK"))||(previus_state.equals("SECOND_CALL_RINGING"))){
                    current_state = "SECOND_CALL_RINGING";
                }

                break;
            case "OFFHOOK":
                Log.d(TAG, "State : offhook, incoming_number : " + incoming_number);
                if((previus_state.equals("IDLE")) ||(previus_state.equals("FIRST_CALL_RINGING")) || previus_state.equals("OFFHOOK")){
                    current_state = "OFFHOOK";
                }
                if(previus_state.equals("SECOND_CALL_RINGING")){
                    current_state ="OFFHOOK";
                    startDialog(context);
                }
                break;
            case "IDLE":
                Log.d(TAG, "State : idle and  incoming_number : " + incoming_number);
                if((previus_state.equals("OFFHOOK")) || (previus_state.equals("SECOND_CALL_RINGING")) || (previus_state.equals("IDLE"))){
                    current_state="IDLE";
                }
                if(previus_state.equals("FIRST_CALL_RINGING")){
                    current_state = "IDLE";
                    startDialog(context);
                }
                updateIncomingNumber("no_number",context);
                Log.d(TAG,"stored incoming number flushed");
                break;
        }
        if(!current_state.equals(previus_state)){
            Log.d(TAG, "Updating  state from "+previus_state +" to "+current_state);
            updateCallState(current_state,context);

        }
    }
    public void startDialog(Context context) {
        Log.d(TAG,"Starting Dialog box");
        Intent intent1 = new Intent(context, NotifyHangup.class);
        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent1);

    }
    public void updateCallState(String state,Context context){
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        spEditor = sp.edit();
        spEditor.putString("call_state", state);
        spEditor.commit();
        Log.d(TAG, "state updated");

    }
    public void updateIncomingNumber(String inc_num,Context context){
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        spEditor = sp.edit();
        spEditor.putString("inc_num", inc_num);
        spEditor.commit();
        Log.d(TAG, "incoming number updated");
    }
    public String getCallState(Context context){
        sp1 = PreferenceManager.getDefaultSharedPreferences(context);
        String st =sp1.getString("call_state", "IDLE");
        Log.d(TAG,"get previous state as :"+st);
        return st;
    }
    public String getIncomingNumber(Context context){
        sp1 = PreferenceManager.getDefaultSharedPreferences(context);
        String st =sp1.getString("inc_num", "no_num");
        Log.d(TAG,"get incoming number as :"+st);
        return st;
    }
}

Even though there is only three states available in android telephony manager,it is very easy to identify state for all situations in a phone call.
So here we are receiving the 3 events from android such as RINGING,OFFHOOK and IDLE. And in order to get the waiting state of a call,we need to define our own states like RINGING, OFFHOOK, IDLE, FIRST_CALL_RINGING, SECOND_CALL_RINGING.
Please think in a way that we are receiving events from android and we will define our on call states.
Here is the method to get call states at onReceive method of broadcast-receiver without registering phonestatelistener and escaping from other complications. See the code.

public class CallListening  extends BroadcastReceiver {
    static CustomPhoneStateListener customPhoneListener;
    private static final String TAG ="broadcast_intent";
    public static String incoming_number;
    private String current_state,previus_state,event;
    public static Boolean dialog= false;
    private Context context;
    private SharedPreferences sp,sp1;
    private SharedPreferences.Editor spEditor,spEditor1;
    public void onReceive(Context context, Intent intent) {
        //Log.d("intent_log", "Intent" + intent);
        dialog=true;
        this.context = context;
        event = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        incoming_number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.d(TAG, "The received event : "+event+", incoming_number : " + incoming_number);
        previus_state = getCallState(context);
        current_state = "IDLE";
        if(incoming_number!=null){
            updateIncomingNumber(incoming_number,context);
        }else {
            incoming_number=getIncomingNumber(context);
        }
        switch (event) {
            case "RINGING":
                Log.d(TAG, "State : Ringing, incoming_number : " + incoming_number);
            if((previus_state.equals("IDLE")) || (previus_state.equals("FIRST_CALL_RINGING"))){
                    current_state ="FIRST_CALL_RINGING";
                }
                if((previus_state.equals("OFFHOOK"))||(previus_state.equals("SECOND_CALL_RINGING"))){
                    current_state = "SECOND_CALL_RINGING";
                }

                break;
            case "OFFHOOK":
                Log.d(TAG, "State : offhook, incoming_number : " + incoming_number);
                if((previus_state.equals("IDLE")) ||(previus_state.equals("FIRST_CALL_RINGING")) || previus_state.equals("OFFHOOK")){
                    current_state = "OFFHOOK";
                }
                if(previus_state.equals("SECOND_CALL_RINGING")){
                    current_state ="OFFHOOK";
                    startDialog(context);
                }
                break;
            case "IDLE":
                Log.d(TAG, "State : idle and  incoming_number : " + incoming_number);
                if((previus_state.equals("OFFHOOK")) || (previus_state.equals("SECOND_CALL_RINGING")) || (previus_state.equals("IDLE"))){
                    current_state="IDLE";
                }
                if(previus_state.equals("FIRST_CALL_RINGING")){
                    current_state = "IDLE";
                    startDialog(context);
                }
                updateIncomingNumber("no_number",context);
                Log.d(TAG,"stored incoming number flushed");
                break;
        }
        if(!current_state.equals(previus_state)){
            Log.d(TAG, "Updating  state from "+previus_state +" to "+current_state);
            updateCallState(current_state,context);

        }
    }
    public void startDialog(Context context) {
        Log.d(TAG,"Starting Dialog box");
        Intent intent1 = new Intent(context, NotifyHangup.class);
        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent1);

    }
    public void updateCallState(String state,Context context){
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        spEditor = sp.edit();
        spEditor.putString("call_state", state);
        spEditor.commit();
        Log.d(TAG, "state updated");

    }
    public void updateIncomingNumber(String inc_num,Context context){
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        spEditor = sp.edit();
        spEditor.putString("inc_num", inc_num);
        spEditor.commit();
        Log.d(TAG, "incoming number updated");
    }
    public String getCallState(Context context){
        sp1 = PreferenceManager.getDefaultSharedPreferences(context);
        String st =sp1.getString("call_state", "IDLE");
        Log.d(TAG,"get previous state as :"+st);
        return st;
    }
    public String getIncomingNumber(Context context){
        sp1 = PreferenceManager.getDefaultSharedPreferences(context);
        String st =sp1.getString("inc_num", "no_num");
        Log.d(TAG,"get incoming number as :"+st);
        return st;
    }
}

不疑不惑不回忆 2024-11-09 19:19:08

您需要一个 PhoneStateListener。这样就可以检查电话呼叫的状态。您必须实现onCallStateChanged。每次电话呼叫状态发生变化时都会调用此方法。然后你可以做这样的事情:

 switch(state) {
            case TelephonyManager.CALL_STATE_IDLE:
                    Log.d("Call","Outgoing Call finished");
            break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.d("Call","Outgoing Call Starting");
            break;
        }

You need a PhoneStateListener. With that is is possible to check the state of a phone call. You have to implement onCallStateChanged. This method is called every time the state of a phone call changes. Then you can do something like this:

 switch(state) {
            case TelephonyManager.CALL_STATE_IDLE:
                    Log.d("Call","Outgoing Call finished");
            break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.d("Call","Outgoing Call Starting");
            break;
        }
离鸿 2024-11-09 19:19:08

我的理解是,除非我们具有内部包访问权限,否则我们无法区分 OFFHOOK 状态内的各种状态。

如果我们具有内部包访问权限,那么只有我们才能知道对方是否实际收到或错过或断开了连接的呼叫。

What I understand is that we cannot differentiate among the various states inside the OFFHOOK state, unless we have the internal package access.

If we have the internal package access then only we can know whether the call connected is actually received or missed or disconnected by the other side.

梦里°也失望 2024-11-09 19:19:08

你必须使用broadcastreceiver和以下onReceive实现来检查phonestate的变化

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;
    final Bundle extras = intent.getExtras();
    if (intent.getAction().equals(
    TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
        final String state = extras.getString(TelephonyManager.EXTRA_STATE);
        if ("IDLE".equals(state)){
            //TODO
        }
        if ("OFFHOOK".equals(state)){
            //TODO
        }
        if ("RINGING".equals(state)){
            //TODO
        }
    }
}

如果你想检查中间状态,你可以添加例如整数形式的状态控制....

you have to use broadcastreceiver with the following onReceive implementation to check for phonestate changes

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;
    final Bundle extras = intent.getExtras();
    if (intent.getAction().equals(
    TelephonyManager.ACTION_PHONE_STATE_CHANGED)){
        final String state = extras.getString(TelephonyManager.EXTRA_STATE);
        if ("IDLE".equals(state)){
            //TODO
        }
        if ("OFFHOOK".equals(state)){
            //TODO
        }
        if ("RINGING".equals(state)){
            //TODO
        }
    }
}

And if you want to check for intermediate state, you can add e.g. state control in form of an integer ....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文