Getting info about your currently running activities

At some point you’ll find yourself needing to get information about the activities running in a certain task.
There’s an easy way of getting a list of running tasks from the ActivityManager service.
You can request a maximum number of tasks running on the phone, and by default, the currently active task is returned first.

Once you have that you can get a ComponentName object by requesting the topActivity from your list.

Here’s an example.

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

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

		Log.d("topActivity", "CURRENT Activity ::"
				+ taskInfo.get(0).topActivity.getClassName());

		ComponentName componentInfo = taskInfo.get(0).topActivity;
      componentInfo.getPackageName();

You will need the following permission on your manifest:
uses-permission android:name=”android.permission.GET_TASKS”

16 comments on “Getting info about your currently running activities”

  1. Alin

    I have an application, where I need to change the language through a
    settings menu. The problem is that the current activity don’t show
    data in new language only if I go back (by back button)to the main
    activity. I ask if there is any solution to reload the current
    activity(and other)…I want anywhere in my app change the language(by
    using a menu)and have data in new language without going back to main
    activity.(I must still in the current activity witch show data and
    resouce in new language)

    This is the code that I write it in every activity:

    new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog,int which) {

    switch(Item_id){

    case 0:
    Toast.makeText(getApplicationContext(),
    Item_name,Toast.LENGTH_SHORT).show();

    // Change locale settings on the device
    locale = new Locale(“En”);
    Locale.setDefault(locale);
    config = new Configuration();
    config.locale = locale;

    getBaseContext().getResources().updateConfiguration(config,
    getBaseContext().getResources().getDisplayMetrics());

    case 1:
    ………………………………………..
    }
    }

  2. noslen

    You can update the the shared preferences and have your activities implement the SharedPreferenceChangeListener.
    SharedPreferences.OnSharedPreferenceChangeListener

    When that trigers you should just be able to update your views in the current activity.

    Hope that helps

  3. gowtham

    hi

    how to find out which activity is currently running and can you explain the concept

    gowtham

  4. noslen

    Hi Gowtham,
    It works like this:
    The activityManager provides a list of tasks that are currently running on your phone.
    A task can be thought of as a collection of related activities. Most commonly your app will have it’s own task.

    You get a reference to the activity manager like this:

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

    One you have that, you can ask the activity manager to give you a list of running tasks.
    In this case I pass a one because I’m only interested in the first running task.
    (The currently running task will always show up first)

    The activity manager gives you this info in a list of activitymanager.runningtaskinfo objects….

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

    The runningtaskinfo object has some additional methods to help you identify your activity. of interest is a reference to the topActivity which provides info about the activity. You can think of topActivity as the most likely activity to have the intent filter of Main or Launcher.

    ComponentName componentInfo = taskInfo.get(0).topActivity;

    I hope that helps.

  5. Suren manawatta

    ActivityManager am = (ActivityManager)context.getSystemService(Activity.ACTIVITY_SERVICE);
    List taskInfo = am.getRunningTasks(5); //1
    for (RunningTaskInfo info : taskInfo) {
    Toast.makeText(context, “\nURRENT Activity:” + info.topActivity.getClassName(), Toast.LENGTH_LONG).show();
    }

  6. Suren manawatta

    use the above code to get the topactivity name when you working inside a BroadcastReceiver
    this is very useful when you develop a SMS service and onReceive() if you wanna find the top activity.

  7. noslen

    @Suren thanks! I’m glad you found it helpful. I appreciate you comment :)

  8. ahmed

    Is there any way to get an instance of the currently running activity , so that i could call methods like dispatch events ?

  9. shahzadrocks

    Thanx Nelson…100% correct.. jUst mention that we will also need uses-permission android:name=”android.permission.GET_TASKS”

  10. noslen

    Hi ahmed,
    Hmm i suppose it COULD be possible. I guess from within a service the trick would be to get access to the context.
    I’m pretty curious about this now. I’m not sure how but I think there could be some tricky way of doing this.

  11. noslen

    Thanks shahzadrocks! I’ve updated the post with your suggestion

  12. chaitic

    hi, is it possible to get farther values with ActivityManager?
    for example, the activity uses a farther class, which is not an activity, but gets a valuable String(name of the item, or something),
    Can I get that String(the name of the current item displayd in a view) also?

  13. Pingback: How to check which activity is showing on screen : Android Community - For Application Development

  14. Pingback: Android: How can I get the current foreground activity (from a service)? - Android Questions - Developers Q & A

  15. nelson

    Hola, buenas.
    Deacuerdo a lo que han posteado en este foro, hay alguna posiblidad de que yo pueda preguntar si cierta aplicación en particular se encuentra en el listado de tareas en proceso?? no importa si se encuentra o no en primer plano, solo busco saber si esta funcionando.
    muchas gracias de ante mano.

  16. nelson

    Hello, good.
    Agree to what they have posted on this forum, is there any possibility that I can ask if some particular application is in the list of tasks in the process? no matter if it is or not in the foreground, just looking to know if it is working.
    thank you very much beforehand.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>