Search code examples
androidandroid-intentphone-call

Recover phone call UI


Basically I'd like to start the current phone call ui from a button in one of my activities.

So far, I've been able to start a new call using

Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:000000"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

or to move to the phonebook using:

Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

I've also tried to use that code, but it seems not to be working on HTC devices and some Huawei (ri is null).

Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER);
intentToResolve.setPackage("com.android.phone");
ResolveInfo ri = getPackageManager().resolveActivity(intentToResolve, 0);
if (ri != null) 
{
    Intent intent = new Intent(intentToResolve);
    intent.setClassName(ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

What I'd like to get is move directly to the call ui. The behaviour is the same as in this procedure:

  1. Be in a phone call
  2. Go back to the Home screen
  3. Expand the notification bar
  4. Click on the call row

I don't know how to achievve and how to make it works on all devices.

Thanks for your precious help.


Solution

  • you have to do as suggested on this answer: android.permission.CALL_PHONE for tablets

    add the call phone permission:

    <uses-permission android:name="android.permission.CALL_PHONE"/>
    

    and then call with Intent.ACTION_CALL.

    Intent intentcall = new Intent(
              Intent.ACTION_CALL, 
              Uri.parse("tel:" + phonenumber));
    startActivity(intentcall);
    

    edit:

    there's a way you could do, but it won't work in Lollipop and forward, as it's been deprecated due to security reasons.

    You could use the ActivityManager to get the running tasks and then analyse the content of each RunningTaskInfo to determine which one is the "phone" and then use the task ID to call moveTaskToFront. It possibly work, but as I said, it will not work with Lollipop and future versions.