2

I've been looking around trying to figure out a way to find out how much time I spend on Whatsapp. I found a couple of apps on the Playstore:

https://play.google.com/store/apps/details?id=com.agrvaibhav.AppUsageTracking&hl=en

The size of such apps is less than 1MB, so I'm guessing its a fairly easy piece of code, but I can't find a way anywhere.

All links either provide a way to let me know how much my own app is being used, or they say this is a possible intrusion of privacy [bizarre!].

The code * # * # 7 8 6 # * # * supposedly is supposed to work, but crashes every time I try.

I have searched hard and can't find a solution anywhere. One possible way I thought of is by running a service that tracks all open apps - but I'm not sure if that's possible in Android.

Md. Sabbir Ahmed
  • 850
  • 8
  • 22
Rishabh Bhardwaj
  • 207
  • 5
  • 14
  • Please don't add voting commentary to posts. It is ideal if people explain their downvotes, but they are not obligated to, and the vast majority of readers - who do not vote - are not interested in this discussion. Add a comment instead, if you wish `:-)`. – halfer Sep 21 '15 at 16:19
  • The claim that allowing apps to monitor the user's usage of other apps could be a privacy violation is not so far-fetched. It is, of course, fine if you explicitly want an app to behave this way, but most people do not know what permissions each app has, or whether apps are using the permissions they have in the spirit of the guidelines Google has set out. – halfer Sep 21 '15 at 16:22
  • this [library](https://github.com/TheBotBox/AppsUsageMonitorAPI) is doing exactly what you are looking for with other additional features too. – bhanu kaushik May 14 '19 at 10:34

3 Answers3

8

Yes, Usage Stats API is the answer. Struggled to figure it out initially, but I have finally managed to do so.

The permission required is :

<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />

Code to get app usage:

 final UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);// Context.USAGE_STATS_SERVICE);
            Calendar beginCal = Calendar.getInstance();
            beginCal.set(Calendar.DAY_OF_MONTH, 11);
            beginCal.set(Calendar.MONTH, 10);
            beginCal.set(Calendar.YEAR, 2015);

            Calendar endCal = Calendar.getInstance();
            endCal.set(Calendar.DAY_OF_MONTH, 12);
            endCal.set(Calendar.MONTH, 10);
            endCal.set(Calendar.YEAR, 2015);

final List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, beginCal.getTimeInMillis(), endCal.getTimeInMillis());
System.out.println("results for " + beginCal.getTime().toGMTString() + " - " + endCal.getTime().toGMTString());
for (UsageStats app : queryUsageStats) {
    System.out.println(app.getPackageName() + " | " + (float) (app.getTotalTimeInForeground() / 1000));
}

Open permissions using :

Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);

This question helped figure it all out. Better documentation on the Android website would have helped greatly.

Here are a couple of links that helped me:

https://github.com/googlesamples/android-AppUsageStatistics

How to use UsageStatsManager?

Community
  • 1
  • 1
Rishabh Bhardwaj
  • 207
  • 5
  • 14
  • 3
    It's only for `>=lollipop` devices . What about pre-**lollipop** devices? – Bhavin Patel Jun 16 '17 at 10:14
  • I want to monitor Facebook, whatsapp, Instagram and Google Search. Were you able to monitor Whatsapp with UsageStats? Did you change any settings in your device or even in the AVD? When the usage access settings Activity opens I don't see any of the apps listed. Very few apps get lised and there is option to list sytem apps, but they are not the apps I am looking at monitoring. – Meenohara Aug 20 '19 at 10:26
1

You can see more like such apps that detects your usage of apps whole week.

Time Track -App Usage Tracker

However while using this app i found that this was not running any background services(checked through running services). So this must be using Usage stats, which is actually a built in mechanism offered by android, which itself keeps tracks of your mobile activities on daily, monthly and yearly basis. Therefore all you need is to use this function, fetch all the data and present it to your user.

https://developer.android.com/reference/android/app/usage/UsageStatsManager#queryUsageStats(int,%20long,%20long)

While for Pre-lollipop devices you may need to run background service in order to keep tracking user activity.

Asim
  • 161
  • 1
  • 12
0

There's actually a new API in Lollipop called the Usage Stats API which could allow an app to track exactly the kind of information you're looking for.

The catch is that using it requires a permission that can be granted only by a system activity, not in the manifest. The docs specify that the system activity may not be present on all devices. Samsung and LG are among the manufacturers who are known to have removed the activity from their builds, so only system apps will ever be able to use the Usage Stats API on their devices.

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82