0

Good day. Can I use the android sdk to get the name of an android application in which the user is working, and the session-time in this application. i.e. service will receive the name of the application and a session and send it to the database.

Solved. Here is my soulution

 private String getRunningAppByActivity(){
        ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
        // get the info from the currently running task
        List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);

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

        final PackageManager pm = context.getPackageManager();
        ApplicationInfo ai;
        try {
            ai = pm.getApplicationInfo(name, 0);
        } catch (final PackageManager.NameNotFoundException e) {
            ai = null;
        }
        final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
        Log.d("APPLICATION_NAME", applicationName);
        return applicationName;
    }
Jenya Kirmiza
  • 701
  • 2
  • 10
  • 27
  • show us your code! <<< you see what I mean? – PKlumpp Aug 07 '14 at 10:36
  • there are tons of similar questions. Research a little :) http://stackoverflow.com/questions/11391451/list-out-installed-running-applications-in-android-programmatically – rupps Aug 07 '14 at 10:46

1 Answers1

1

try this:

     ActivityManager activityManager = (ActivityManager)            
     {context}.getSystemService(Context.ACTIVITY_SERVICE);
     List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

    final PackageManager pm = getApplicationContext().getPackageManager();

    for (RunningTaskInfo task : tasks) {

    try {
        ai = pm.getApplicationInfo(task.baseActivity.getPackageName(), 0);
       } catch (final NameNotFoundException e) {
    ai = null;
     }
      String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai): "(unknown)");

    }
Xenione
  • 2,174
  • 1
  • 23
  • 30