-5

I need to build a android app that checks how much time you spend on a selected app X, if the consumed time is more than a preset value - the app will show a message every time you run X and X will exit.

As an example, you set in our app that the maximum allowed time for WhatsApp is 20 minutes per day. If you consumed the 20 minutes and you try to open WhatsApp - you will get a message and WhatsApp task will be closed.

Is it possible? If yes then how?

Please let me know with some sample code.

  • 2
    Sample code? Sorry. Can you show us what have you tried so far? – Rohit5k2 Aug 20 '15 at 13:02
  • And yes your question "track time spent on an app" has this answer http://stackoverflow.com/questions/16397784/measure-time-spent-on-android-applications – Rohit5k2 Aug 20 '15 at 13:03
  • 1
    Yes it is possible. Set the start time on the onCreate Method of the application or on the main activity. Or start a timer. I will not add sample code. Why i should do you work? – Zelldon Aug 20 '15 at 13:04
  • @Zelldon: He is asking if he can track the usage of another app. I have gave him the link I found on SO after a simple search. :D – Rohit5k2 Aug 20 '15 at 13:13
  • @Rohit5k2 : I am new to android. https://github.com/googlesamples/android-AppUsageStatistics I used this sample, but it return "Last time used" not usage time, Please advise. – Mahmoud Sengab Aug 20 '15 at 13:26
  • Did you try the link i gave? See this answer and try to implement it http://stackoverflow.com/a/16398407/2936153 – Rohit5k2 Aug 20 '15 at 13:28

1 Answers1

0

You can use UsageStats to get the spent time for any app. Check this link - https://developer.android.com/reference/android/app/usage/UsageStats.html#getTotalTimeInForeground()

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Shweta
  • 349
  • 1
  • 2
  • 15