Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android API to check if call is Active or On Hold

Tags:

android

Is there an API function to check if a call is currently Active, or if has been put on Hold?

Assuming I have two connected calls, is there a way to check if each one is active, on-hold, or maybe they are connected in a conference call?

like image 258
DanJ Avatar asked Oct 14 '11 10:10

DanJ


2 Answers

Yes, you can check if a call is active over device or not:

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;
   }
}
like image 159
Vineet Shukla Avatar answered Nov 18 '22 08:11

Vineet Shukla


This is proper way:

  1. Add a permission to manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
  1. Request permission from the user:
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. And finally, check if device handles ongoing call anytime:
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

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

like image 40
matusalem Avatar answered Nov 18 '22 08:11

matusalem