Android 应用使用情况统计
本篇文章简单记录下android中usageStatsManager的简单使用.
UsageStatsManager提供对设备使用历史记录和统计信息的访问,时间周期为days, weeks, months, and years.
1: 权限申请
<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
2: 授予权限
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
3: 获取应用使用时长
// 获取UsageStatsManager实例
UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
// 获取当前时间和过去一段时间的时间戳
Calendar calendar = Calendar.getInstance();
long endTime = calendar.getTimeInMillis();
calendar.add(Calendar.DAY_OF_WEEK, -1);
long startTime = calendar.getTimeInMillis();
// 查询应用使用时长
List<UsageStats> usageStatsList = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, startTime, endTime);
// 遍历应用使用时长列表
for (UsageStats usageStats : usageStatsList) {
// 获取应用包名
String packageName = usageStats.getPackageName();
// 获取应用使用时长(以毫秒为单位)
long totalTimeInForeground = usageStats.getTotalTimeInForeground();
if (totalTimeInForeground>0)
// 输出应用包名和使用时长
Log.d("XXXX", "pkgName: " + packageName + ", totalTimeInForeground: " + totalTimeInForeground/1000);
}
getTotalTimeInForeground 方法是获取应用在前台的使用时长,单位毫秒.文章来源:https://www.toymoban.com/news/detail-829249.html
queryUsageStats 可以指定时间间隔查询:文章来源地址https://www.toymoban.com/news/detail-829249.html
/**
* An interval type that spans a day. See {@link #queryUsageStats(int, long, long)}.
*/
public static final int INTERVAL_DAILY = 0;
/**
* An interval type that spans a week. See {@link #queryUsageStats(int, long, long)}.
*/
public static final int INTERVAL_WEEKLY = 1;
/**
* An interval type that spans a month. See {@link #queryUsageStats(int, long, long)}.
*/
public static final int INTERVAL_MONTHLY = 2;
/**
* An interval type that spans a year. See {@link #queryUsageStats(int, long, long)}.
*/
public static final int INTERVAL_YEARLY = 3;
/**
* An interval type that will use the best fit interval for the given time range.
* See {@link #queryUsageStats(int, long, long)}.
*/
public static final int INTERVAL_BEST = 4;
4: 测试输出
2024-02-19 16:29:10.489 13045-13045/com.test.usagetest D/XXXX: Package Name: com.coloros.wirelesssettings, Total Time: 7
2024-02-19 16:29:10.489 13045-13045/com.test.usagetest D/XXXX: Package Name: com.test.usagetest, Total Time: 336
2024-02-19 16:29:10.490 13045-13045/com.test.usagetest D/XXXX: Package Name: android, Total Time: 103
2024-02-19 16:29:10.490 13045-13045/com.test.usagetest D/XXXX: Package Name: com.test.android, Total Time: 2585
2024-02-19 16:29:10.490 13045-13045/com.test.usagetest D/XXXX: Package Name: com.google.android.captiveportallogin, Total Time: 12
2024-02-19 16:29:10.490 13045-13045/com.test.usagetest D/XXXX: Package Name: com.oppo.launcher, Total Time: 170
2024-02-19 16:29:10.490 13045-13045/com.test.usagetest D/XXXX: Package Name: com.coloros.digitalwellbeing, Total Time: 49
2024-02-19 16:29:10.490 13045-13045/com.test.usagetest D/XXXX: Package Name: com.tencent.mobileqq, Total Time: 2557
2024-02-19 16:29:10.490 13045-13045/com.test.usagetest D/XXXX: Package Name: com.android.packageinstaller, Total Time: 9
2024-02-19 16:29:10.490 13045-13045/com.test.usagetest D/XXXX: Package Name: com.test.tools, Total Time: 9
2024-02-19 16:29:10.490 13045-13045/com.test.usagetest D/XXXX: Package Name: com.android.settings, Total Time: 106
2024-02-19 16:29:10.490 13045-13045/com.test.usagetest D/XXXX: Package Name: com.android.vpndialogs, Total Time: 2
到了这里,关于Android 应用使用情况统计的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!