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?
BroadcastReceiver, check the current on-screen activity which is other than
To check on-screen activity, get the top activity using
ActivityManagerby getting running task list.
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:
Note: the above functionality is deprecated in
Marshmallow version. I just giving you here an example by code snippet.