如何获取拨出 android 呼叫的呼叫状态
How to get call state of outgoing android call
我正在从 Android Studio 发起呼叫。代码如下:
我想随时了解通话状态。 link:https://developer.android.com/reference/android/telecom/Call.html
显示调用的状态可以通过使用 Class 调用获得。所以如果我使用 Call.getState() 我应该能够获得当前状态。但是我得到编译错误:
Error:(28, 20) 错误:Call() 不是 public in Call;无法从外部包访问。枚举中定义了几种呼叫状态:Dialing、Ringing、Connected、DIsconnected、Holding 等。
当我 运行 代码时,它确实进行了调用,因为我可以看到模拟器进行调用的屏幕。
开发人员指南未提供任何使用这些 类 的示例。
谢谢你的帮助。
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);
}
}
并注册状态监听器:
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
试试这样用(没试过)-
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
}
}
};
我正在从 Android Studio 发起呼叫。代码如下: 我想随时了解通话状态。 link:https://developer.android.com/reference/android/telecom/Call.html 显示调用的状态可以通过使用 Class 调用获得。所以如果我使用 Call.getState() 我应该能够获得当前状态。但是我得到编译错误: Error:(28, 20) 错误:Call() 不是 public in Call;无法从外部包访问。枚举中定义了几种呼叫状态:Dialing、Ringing、Connected、DIsconnected、Holding 等。 当我 运行 代码时,它确实进行了调用,因为我可以看到模拟器进行调用的屏幕。
开发人员指南未提供任何使用这些 类 的示例。 谢谢你的帮助。
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);
}
}
并注册状态监听器:
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
试试这样用(没试过)-
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
}
}
};