0

When i start application from another one through

Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory("android.intent.category.LAUNCHER");
            intent.setComponent(new ComponentName("com.app.app", "com.app.app.Main"));
            startActivity(intent);




ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> runningProcInfo = activityManager.getRunningAppProcesses();

Log.e("TAG", "runningProcInfo.get(0).processName "+runningProcInfo.get(0).processName);

And I am getting Home application package

What is wrong in my code?

drifter
  • 683
  • 1
  • 11
  • 24

2 Answers2

1

There is nothing wrong with the code.

From the ActivityManager.getRunningAppProcesses() documentation: "Returns a list of RunningAppProcessInfo records, or null if there are no running processes (it will not return an empty list). This list ordering is not specified."

So, runningProcInfo.get(0) could be any process that's currently running, not necessarily the foreground one.

Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
0
List<ActivityManager.RunningAppProcessInfo> mRunningProcessInfo;
        mRunningProcessInfo = mActivityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo process : mRunningProcessInfo) {
            if(process.importance & process.IMPORTANCE_FOREGROUND==1)
            //process.processName is the process in foreground
        }
Rasel
  • 15,499
  • 6
  • 40
  • 50