Issue
I have an application that keeps tracks of other applications screentime. I have a service which is supposed to give a notification after the user has spend X time on a specific application. To keep track of other applications, I have used a Service. When my own application is open, the details of other applications screen time is shown correctly, but when my app goes in the background, the details aren't updated (they remain the same as when my own application was open), but as soon as I reopen my application, the details get updated. Why isnt the UsageStatsManager working properly while being in the background?
var appList = usm.queryUsageStats(
UsageStatsManager.INTERVAL_DAILY,
System.currentTimeMillis() - 1000 * 3600 * 24,
System.currentTimeMillis()
)
I have the following code in my manifest
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
<service android:enabled="true" android:name=".MyService" />
</application>
Solution
UsageStatsManager doesn't update the unless the app goes in background.
You can archive this by adding the difference of current time and timestamp of current running app going foreground.
Refer to this answer for details : How to count app usage time while app is on foreground?
Answered By - Nitish
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.