简体   繁体   English

如何获取传出android通话的通话状态

[英]How to get call state of outgoing android call

I am originating a call from Android Studio. 我正在从Android Studio发起呼叫。 The code is as follows: I want to get the state of the call at any point. 代码如下:我想随时获取调用状态。 The link: https://developer.android.com/reference/android/telecom/Call.html shows the state of the call can be obtained by using the Class call.. SO if I use Call.getState() I should be able to get the current state. 链接: https : //developer.android.com/reference/android/telecom/Call.html显示可以通过使用Class调用获得调用的状态。因此,如果我使用Call.getState(),我应该能够获取当前状态。 But I get the compilation error: Error:(28, 20) error: Call() is not public in Call; 但是我收到编译错误: 错误:(28,20)错误:Call()在Call中不是公共的; cannot be accessed from outside package . 无法从外部包访问 There are several call states defined in the enum: Dialing, Ringing, Connected, DIsconnected, Holding, etc. When I run the code, it does make the call as I can see the screen of emulator making the call. 枚举中定义了几种呼叫状态:拨号,振铃,已连接,Disconnected,保持等。当我运行代码时,它会进行呼叫,因为我可以看到仿真器进行呼叫的屏幕。

The developer guide does not provide any examples of using these classes. 开发人员指南未提供使用这些类的任何示例。 Thank you for your help. 谢谢您的帮助。

package com.example.ramesh.makeacall;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telecom.Call;
import android.telephony.*;

import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Call call;
        call =  new Call();
        call();

    }
    private void call() {

        try {

            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:5555551212"));
            System.out.println("====before startActivity====");
            startActivity(callIntent);


        } catch (ActivityNotFoundException e) {
            Log.e("helloAndroid","Call failed",e);
        }
    }

    }
 public class MyPhoneStateListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                handleRinging(incomingNumber);
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                handleOffHook();
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                handleIdle();
                break;
        }
        super.onCallStateChanged(state, incomingNumber);
    }
}

and register statelistener : 并注册statelistener:

telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); telephonyManager =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); telephonyManager.listen(myPhoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);

Try to use like this(Haven't tried it though) - 尝试这样使用(虽然没有尝试过)-

Call.Callback callback = new Call.Callback() {
    @Override
    public void onStateChanged(Call call, int state) {
        super.onStateChanged(call, state);
        if(state == Call.STATE_RINGING){
            //you code goes here
        }
    }
};

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

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