1

everyone. I have an assignment where I need to get some user data using android, and one of the data items that I must retrieve is the name of the application that is currently being used by the user eg. Say the user is using the Camera application, I need to be able to show that the user is using the Camera application using code. Is there any way do this? Any help would be much appreciated.

D Brown
  • 460
  • 3
  • 8
  • 22

2 Answers2

0

You can try this code it gives the package of the currently active screen:

ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);

// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);

ComponentName componentInfo = taskInfo.get(0).topActivity;
if(!componentInfo.getPackageName().equals("your.package.name")) {
    //Do your stuff
}

You might need the permission

<uses-permission android:name="android.permission.GET_TASKS" />
activesince93
  • 1,716
  • 17
  • 37
0

This method will be helpful , ActivityManager.getRunningAppProcesses() check this ,

Get active Application name in Android

Community
  • 1
  • 1
sat
  • 40,138
  • 28
  • 93
  • 102