如何获取传出android调用的调用状态

时间:2016-12-15 03:19:08

标签: android state phone-call

我发起了Android Studio的电话。代码如下: 我希望随时获得通话状态。链接:https://developer.android.com/reference/android/telecom/Call.html 显示调用的状态可以通过使用Class调用获得..如果我使用Call.getState()我应该能够获得当前状态。但是我得到了编译错误: 错误:(28,20)错误:Call()在Call中不公开;无法从外部包访问。枚举中定义了几种呼叫状态:拨号,振铃,连接,DI连接,保持等。 当我运行代码时,它确实进行了调用,因为我可以看到模拟器的屏幕进行调用。

开发人员指南未提供使用这些类的任何示例。 谢谢你的帮助。

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

    }

2 个答案:

答案 0 :(得分:0)

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

并注册statelistener:

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

答案 1 :(得分:0)

尝试使用这样(虽然没试过) -

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
        }
    }
};
相关问题