Android API用于检查呼叫是处于活动状态还是处于保持状态

时间:2011-10-14 10:36:57

标签: android

是否有API函数来检查呼叫当前是否处于活动状态,或者是否已暂停?

假设我有两个已连接的呼叫,有没有办法检查每个呼叫是否处于活动状态,是否处于保持状态,还是可能已在电话会议中连接?

2 个答案:

答案 0 :(得分:30)

是的,您可以检查呼叫是否在设备上处于活动状态:

public static boolean isCallActive(Context context){
   AudioManager manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
   if(manager.getMode()==AudioManager.MODE_IN_CALL){
         return true;
   }
   else{
       return false;
   }
}

答案 1 :(得分:1)

这是正确的方法:

  1. 添加显示权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
  1. 请求用户许可:
private boolean runThisWhileStartingApp() {
  boolean hasPhonePermission = checkPermission(android.Manifest.permission.READ_PHONE_STATE, "Explantation why the app needs this permission");
  if (!hasPhonePermission) {
    // user did not allow READ_PHONE_STATE permission
  }
}

private boolean checkPermission(final String permissionName, String reason) {
  if (ContextCompat.checkSelfPermission(MyActivity.this, permissionName) != android.content.pm.PackageManager.PERMISSION_GRANTED) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(MyActivity.this, permissionName)) {
      AlertDialog alertDialog = new AlertDialog.Builder(MyActivity.this).create();
      alertDialog.setTitle(permissionName);
      alertDialog.setMessage(reason);
      alertDialog.setCancelable(false);
      alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
              dialog.dismiss();
              ActivityCompat.requestPermissions(MyActivity.this, new String[]{ permissionName }, 1000);
            }
          });
      alertDialog.show();
    } else {
      ActivityCompat.requestPermissions(InitActivity.this, new String[]{ permissionName }, 1000);
    }
    return false;
  }
  return true;
}
  1. 最后,检查设备是否随时处理正在进行的呼叫:
TelecomManager tm = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
boolean isInCall = tm.isInCall(); // true if there is an ongoing call in either a managed or self-managed ConnectionService, false otherwise

文档:https://developer.android.com/reference/android/telecom/TelecomManager#isInCall()