2

I have created an app that runs a service to read which app/activity user have opened and using it at the current time. The problem is that the service reads only the launcher application. It doesn't return me the cirrently open app/activity. Can you help? The code i write is below. Thanks in advance.

@Override
public void onStart(Intent intent, int startId) {
    // For time consuming an long tasks you can launch a new thread here...
    Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();


    Runnable runable = new Runnable() {


        public void run() {
            try{
                ActivityManager am2 = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
                String packageName = am2.getRunningTasks(1).get(0).topActivity
                        .getPackageName();

                    Log.w("RunningTask", packageName);

                handler.postDelayed(this, 8000);
            }
            catch (Exception e){

            }


        }
    };
    handler.postDelayed(runable, 8000);




}
nestorasg
  • 119
  • 2
  • 13
  • try to be a bit more explicit about what you really want to do. For example if would really like to know what the user did or where the user went, I would use the observer pattern for storing and notifying listeners when there is an update. The listener interface would be implemented by implemented by all the packages, or classes that need to notify when the user has touched them. – Andrei T Sep 17 '15 at 16:17

1 Answers1

0

If you use Android 5.0 or above, getRunningTasks() is deprecated and will only return a small subset, including the caller's own tasks, and possibly some other tasks such as home.

You may check out getRunningAppProcesses() which worked before Android 5.1.1. See Cannot get foreground activity name in Android Lollipop 5.0 only and Android 5.1.1 and above - getRunningAppProcesses() returns my application package only

Example using getRunningAppProcesses:

ActivityManager am2 = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
String processName = am2.getRunningAppProcesses().get(0).processName;
Log.w("Running process", processName);
Community
  • 1
  • 1
headuck
  • 2,763
  • 16
  • 19
  • well i'm using Android 5.0.1. I have to replace only the `getRunningTasks()` with `getRunningAppProcesses`? – nestorasg Sep 17 '15 at 16:09
  • See above. Please note that the function returns process name rather than package name. See http://stackoverflow.com/questions/27571889/is-process-name-same-as-package-name-in-android – headuck Sep 17 '15 at 16:19
  • but i want to get the package name – nestorasg Sep 17 '15 at 16:23
  • 1
    am2.getRunningAppProcesses().get(0).pkgList is a String[] of the package names of the process. See http://developer.android.com/reference/android/app/ActivityManager.RunningAppProcessInfo.html – headuck Sep 17 '15 at 16:39
  • I found the solution based on your answer, thank you. `ActivityManager am2 = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE); String processName[] = am2.getRunningAppProcesses().get(0).pkgList; Log.w("Running process", processName[0]);` – nestorasg Sep 17 '15 at 20:32