0

I got code that will display all apps running on device (package name and app name). I need time how long they have been used. How I can do that? Please.

     ActivityManager manager =(ActivityManager)getSystemService(ACTIVITY_SERVICE);
    final List<ActivityManager.RunningAppProcessInfo> runningProcesses = manager.getRunningAppProcesses();
    StringBuilder b = new StringBuilder();
    PackageManager pm = this.getPackageManager();
    for (ActivityManager.RunningAppProcessInfo process: runningProcesses)
    {
        try
        {
            b.append (pm.getApplicationLabel(pm.getApplicationInfo(process.processName, PackageManager.GET_META_DATA)));
        }
        catch (Exception e)
        {
            b.append("Unknown");
        }
        b.append(System.getProperty("line.separator"));
        b.append(process.processName);
        b.append(System.getProperty("line.separator"));
        //here the run time of an application
        b.append(System.getProperty("line.separator"));
        b.append("---------------------------------------------------");
        b.append(System.getProperty("line.separator"));
    }
    processesShow=(TextView) this.findViewById(R.id.citac);
    processesShow.setText(b.toString());
  • i`m not sure that i understand your questions right, you means need to check the other applications running time on device, like 3hrs or means the installed log like 2014/01/01~2014/02/02 ? – sique Mar 05 '15 at 02:47
  • @sique I mean time like 3hrs. It will be like Google Chrome Used for 3 hrs com.google.chrome some thing like that. – Bohumil Kulhanek Mar 05 '15 at 10:38

1 Answers1

0

If you want to keep track of the time while the app is open you can use timer

And if you want to view for how long since the app is installed then using Date

1.Check the date when the app is installed

2.Store it

3.When user opens the app check current date

4.Compare it and set text to how long

few links on using and comparing dates

http://www.mysamplecode.com/2011/10/android-get-current-time-and-date.html

Best way to compare dates in Android

If you want to use timer then you should do something like this:

    public class MainActivity extends ActionBarActivity {

    TextView timer;
    private long startTime = 0L;
    private Handler customHandler = new Handler();
    long timeInMilis = 0L;
    long timeSwapBuff = 0L;
    long updateTime = 0L;
    int p= 0;
    int S_secs_int;
    String s_p;
    String S_secs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button = (ImageView) findViewById(R.id.button);
        timer = (TextView) findViewById(R.id.timer);
        checkTime();




    }


    public  void checkTime(){


            p++;
            points.setText(""+p);

            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);

    }

    private Runnable updateTimerThread = new Runnable() {
        @Override
        public void run() {

            timeInMilis = SystemClock.uptimeMillis() - startTime;
            updateTime= timeSwapBuff + timeInMilis;

            int secs = (int) (updateTime/1000);
            int mins = secs /60;
            secs = secs % 60;

            int milliseconds = (int) (updateTime % 1000);
            S_secs = String.format("%05d",secs);

            timer.setText(S_secs);

            customHandler.postDelayed(this, 0);

        }
    };

  }
Community
  • 1
  • 1
ii7scw
  • 351
  • 1
  • 3
  • 17
  • I want to track time while the app is open. Like this app [link](https://play.google.com/store/apps/details?id=com.agrvaibhav.AppUsageTracking&hl=en) . So shall I use timer? I was trying to use getElapsedCpuTime() but it tracks even when the app is in background and it didn't work out well. – Bohumil Kulhanek Mar 05 '15 at 10:55
  • or like this app [link](https://play.google.com/store/apps/details?id=com.appuccino.frequencyfree) – Bohumil Kulhanek Mar 05 '15 at 11:01
  • You should use timer for this(get the time spent while the app is on) – ii7scw Mar 05 '15 at 11:48
  • Is there any way to implement it to my code? I am quite new to android programming. – Bohumil Kulhanek Mar 05 '15 at 14:06
  • Yes there is but you should see how and what you'll need.. and if you want ot store the time and maybe compare it later then you should use SharedPreferences – ii7scw Mar 05 '15 at 20:05
  • I don't want to compare time. I just want to keep track on time the user spends on every app. – Bohumil Kulhanek Mar 05 '15 at 22:10
  • Okay,but you stil have to use SharedPreferences to store that value Example...You're using Messenger for 50 mins,on exit you need to store that time and when you go into your app you can get the value from teh saved preferences and display it Get it? – ii7scw Mar 05 '15 at 22:20