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?
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.
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
}
精彩评论