Track usage of each Android app on devices up to Lollipop

I would like to create an application that can track how much time a user spends on certain applications (mainly to prove the concept and for implementation in other applications.) I know that Lollipop has implemented the UsageStatsManager API, which allows you to track the β€œtotal time period when the application was in the foreground for the time interval (by day, week, month or year). "

I also read that one can continuously try AcvtivtyManager , however this will lead to the abolition of the battery life and will take up the processor and RAM.

I wonder if there is an effective way to track the amount of time an application has opened on devices with an Android version below Lollipop. In particular, like applications such as Aptrax and App Usage Tracker , as well as many others, do this with very small ones or "No battery drain (even with high frequency tracking)." Is there any other method that these applications use to get data on application usage on devices before Lollipop, with little battery use or polling the Activity box not as much as the source I read, making me believe.

+6
source share
1 answer

I tried to do the same, but did not find a better way to rashly ask the activity manager, now, so as not to waste time using alarmManager and recieverto, set the pollin wake-up time so that when the phone sleeps it will not be woken up for your survey (suppose that the user uses applications when the screen is on and the phone is active) and polling like every 5 seconds or so here is a basic code example

Intent alarmIntent = new Intent(context, MyReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis(), pendingIntent); public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) { // poll activity manager and any thing else you want here // including figuring out the next time you want to run // and scheduling another PendingIntent with the AlarmManager } } 

Using AlarmManager.RTC, make sure that the receiver only polls when the phone is awake.

+3
source

Source: https://habr.com/ru/post/980891/


All Articles