Monday, June 6, 2011

How to detect which is the current top activity.

Here is one of the way you can detect


public boolean whatIsCurrentActivity()
{
ActivityManager am = (ActivityManager) mContext.getSystemService(mContext.ACTIVITY_SERVICE);
List ActivityManager.RunningTaskInfo taskInfo = am.getRunningTasks(1);
if(taskInfo != null ){
System.out.println("Top activity - Package name of the process is "+taskInfo.get(0).topActivity.getPackageName() );
}

2 comments:

  1. Very useful, always important to check null although ideally there should never not be a top Activity.

    ReplyDelete
  2. its returning my service's package . i need the package of application open just beneth the service.

    ReplyDelete