1

Please suggest me how can I get current open application name, even if there is home screen on device then I will find "Home screen is open".

Simon Gates
  • 23
  • 1
  • 1
  • 11
  • 1
    you can get the activity by http://stackoverflow.com/questions/3873659/android-how-can-i-get-the-current-foreground-activity-from-a-service – Dheeresh Singh Jun 20 '12 at 11:42

5 Answers5

2

Use an AccessibilityService

  • You can detect the currently active window by using an AccessibilityService.
  • In the onAccessibilityEvent callback, check for the TYPE_WINDOW_STATE_CHANGED event type to determine when the current window changes.
  • Check if the window is an activity by calling PackageManager.getActivityInfo().

I tested and working in Android 2.2 (API 8) through Android 7.1 (API 25).

public class MyAccessibilityService extends AccessibilityService {
    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();

        //Configure these here for compatibility with API 13 and below.
        AccessibilityServiceInfo config = new AccessibilityServiceInfo();
        config.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
        config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;

        if (Build.VERSION.SDK_INT >= 16)
            //Just in case this helps
            config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;

        setServiceInfo(config);
    }
    @Override
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        Log.d("ABC-",accessibilityEvent.getPackageName()+" -- "+accessibilityEvent.getClassName());
        if (accessibilityEvent.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            if (accessibilityEvent.getPackageName() != null && accessibilityEvent.getClassName() != null) {
                ComponentName componentName = new ComponentName(
                        accessibilityEvent.getPackageName().toString(),
                        accessibilityEvent.getClassName().toString()
                );

                ActivityInfo activityInfo = tryGetActivity(componentName);
                boolean isActivity = activityInfo != null;
                if (isActivity)
                    Log.i("CurrentActivity", componentName.flattenToShortString());
            }
        }
    }

    private ActivityInfo tryGetActivity(ComponentName componentName) {
        try {
            return getPackageManager().getActivityInfo(componentName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
    }
    @Override
    public void onInterrupt() {

    }
}
Trung Đoan
  • 643
  • 7
  • 18
1

You can also list running tasks with the code below:

ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
List li = am.getRunningTasks(100);
Iterator i = li.iterator();
PackageManager pm = getApplicationContext().getPackageManager();
while (i.hasNext()) {
    try {
        ActivityManager.RunningTaskInfo info = (ActivityManager.RunningTaskInfo)(i.next());
        String ac = info.baseActivity.getPackageName();
        CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
        ac, PackageManager.GET_META_DATA));
        Log.v("asd", c.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Faruk Toptas
  • 1,257
  • 14
  • 21
0

For me the above example did not work. So i ended up using this:

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

    List<ActivityManager.RecentTaskInfo> l = am.getRecentTasks(1,
            ActivityManager.RECENT_WITH_EXCLUDED);
    Iterator<ActivityManager.RecentTaskInfo> i = l.iterator();

    PackageManager pm = this.getPackageManager();

    while (i.hasNext()) {
        try {
            Intent intent = i.next().baseIntent;
            List<ResolveInfo> list = pm.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);

            CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
                    list.get(0).activityInfo.packageName,
                    PackageManager.GET_META_DATA));

            Toast.makeText(this, "Application name: " + c.toString(),
                    Toast.LENGTH_LONG).show();

        } catch (Exception e) {
            Toast.makeText(this,
                    "Application name not found: " + e.toString(),
                    Toast.LENGTH_LONG).show();
        }
    }
-1

With this you can get the current application name

 Resources appR = getApplicationContext().getResources(); 
 CharSequence txt = appR.getText(appR.getIdentifier("app_name","string", getApplicationContext().getPackageName()));
 System.out.println(txt+"  APp Name");
atluriajith
  • 762
  • 3
  • 17
  • 41
  • hey thanks for your reply but let me know that if I run this in background then it give other opened application name that currently present on the screen? – Simon Gates Jun 20 '12 at 12:09
-2

Simply using this code

getActivity().getApplicationInfo().packageName

It gives you application info, and then call the package name.

azwar_akbar
  • 1,451
  • 18
  • 27