Search code examples
androidincoming-call

How to know which app is opening when an incoming call come?


I'm using a broadcast receiver to get notified when there is an incoming call. Now I need to know if a specific app, say Facebook, is opening when the call happens to do my work.

Is this possible?


Solution

  • After receiving BroadcastReceiver, check the current on-screen activity which is other than Home screen.

    To check on-screen activity, get the top activity using ActivityManagerby getting running task list.

    For example:

    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(2);
    ComponentName componentInfo = taskInfo.get(1).topActivity;
    String callHandlerPackageName = componentInfo.getPackageName();
    
    Log.d("Call_incoming", "Call_receiver_app ::" + callHandlerPackageName);
    

    You will need the following permission on your manifest:

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

    Note: the above functionality is deprecated in Marshmallow version. I just giving you here an example by code snippet.