0

I want to know what activity is being displayed in android device

if the activity that I will search I execute code if not I execute another code

purpose is to make an android service to check this task

user2282259
  • 155
  • 1
  • 3
  • 9

3 Answers3

0

This should work for you:

Get active Application name in Android

Just tweak it with the functionality you want, and you should be able to check rather easily.

Community
  • 1
  • 1
Michell Bak
  • 13,182
  • 11
  • 64
  • 121
  • I want to do with the activitéManager what the activity during execution I want to compare with another activity (exp: MainAlarme) – user2282259 Apr 21 '13 at 23:03
0

Are you sure that's what you want?

purpose is to make an android service to check this task

If you told us what was the reason behind your "purpose", I'm almost sure we could help you with a better way of doing what you want (probably we could find a way which would waste less battery and which would antagonize less your users).

In any case, here is the answer to your question:

Use getRunningTasks of ActivityManager to get a list of the recently launched activities.

public List getRunningTasks (int maxNum)

Return a list of the tasks that are currently running, with the most recent being first and older ones after in order. Note that "running" does not mean any of the task's code is currently loaded or activity -- the task may have been frozen by the system, so that it can be restarted in its previous state when next brought to the foreground.

You might also want to use the flag RECENT_WITH_EXCLUDED to make sure you get all the activities, even the ones that purposefully exclude themselves from that list.

public static final int RECENT_WITH_EXCLUDED

Added in API level 1 Flag for use with getRecentTasks(int, int): return all tasks, even those that have set their FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS flag.

Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49
  • I want to do with the activitéManager what the activity during execution I want to compare with another activity (exp: MainAlarme) – user2282259 Apr 21 '13 at 23:03
  • Activité? Do you speak French by any chance? My own native language is French so I can switch to that language if need be (and translate for the rest of us). Why do you want to compare the activity with MainAlarm? Did you know that the alarm clock had its own Content Provider that you could manipulate the values of? What kind of application are you trying to make? – Stephan Branczyk Apr 22 '13 at 04:45
0

I found this code it works very well

Log.i("---------------", "----------onCreate--------------");

        Log.i("-----test------", "----------1--------------");
         ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE);
         Log.i("-----test------", "----------2-------------");
           List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
           Log.i("-----test------", "---------3--------------");
            ComponentName componentInfo = taskInfo.get(0).topActivity;
            Log.i("-----test------", "----------4--------------");
            Log.i("------------------", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName()+"   Package Name :  "+componentInfo.getPackageName());
            Log.i("-----test------", "----------5--------------");
user2282259
  • 155
  • 1
  • 3
  • 9