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

Dan*_*anJ 17 android

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

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

Vin*_*kla 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;
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 我不认为这是可行的方法,因为音频管理器也可以被其他应用程序用于为自己的目的设置不同的模式(如mode_normal或mode_in_call).这样,上面的代码将没有用处. (3认同)
  • 这并不能确定电话是否有效; 但幸运的是,这种方法也可以查找VOIP呼叫是否也处于活动状态. (2认同)

mat*_*lem 5

这是正确的方法:

  1. 添加清单权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Run Code Online (Sandbox Code Playgroud)
  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;
}
Run Code Online (Sandbox Code Playgroud)
  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
Run Code Online (Sandbox Code Playgroud)

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