cjy5066 2023-09-25 14:48 采纳率: 88.2%
浏览 8
已结题

(标签-List|关键词-Map)

需求:安卓开发 获取手机中最常用的app信息。获取的结果为0(小米手机和AS自带的模拟器)
版本:安卓11
下面是我的代码

img


public class TextActivity extends AppCompatActivity {

    private final String[] PERMISSIONS_CONTACTS = new String[]{
            android.Manifest.permission.INTERNET,
            android.Manifest.permission.GET_PACKAGE_SIZE,
            android.Manifest.permission.QUERY_ALL_PACKAGES,
            android.Manifest.permission.PACKAGE_USAGE_STATS
    };

    private final int app_code = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);

  // 判断是否开启权限
        PermissionUtil.checkPermission(this, PERMISSIONS_CONTACTS, app_code);

        TextView textView = findViewById(R.id.tv_size);

        List<AppUsageInfo> appUsageInfoList = getTopUsedApps(this, 5); // 获取前5个最常用的应用程序信息
        textView.setText("数量:"+appUsageInfoList.size()+"==");
        for (AppUsageInfo appUsageInfo : appUsageInfoList) {
            Log.d("AppInfo", appUsageInfo.getAppName() + ": " + appUsageInfo.getTotalTimeInForeground() + "毫秒");
        }
    }

    public static List<AppUsageInfo> getTopUsedApps(Context context, int limit) {
        UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
        PackageManager packageManager = context.getPackageManager();

        Calendar calendar = Calendar.getInstance();
        long endTime = calendar.getTimeInMillis();
        calendar.add(Calendar.DAY_OF_YEAR, -1); // 查询一天内的应用使用情况
        long startTime = calendar.getTimeInMillis();

        UsageEvents usageEvents = usageStatsManager.queryEventsForSelf(startTime, endTime);

        List<UsageStats> usageStatsList = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, startTime, endTime);
        Map<String, UsageStats> usageStatsMap = usageStatsManager.queryAndAggregateUsageStats(startTime, endTime);

        List<AppUsageInfo> appUsageInfoList = new ArrayList<>();

        for (UsageStats usageStats : usageStatsList) {
            try {
                String packageName = usageStats.getPackageName();
                Intent intent = packageManager.getLaunchIntentForPackage(packageName);
                if (intent != null) {
                    String appName = packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, 0)).toString();
                    long totalTimeInForeground = usageStatsMap.get(packageName).getTotalTimeInForeground();
                    appUsageInfoList.add(new AppUsageInfo(appName, packageName, totalTimeInForeground));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        Collections.sort(appUsageInfoList, new Comparator<AppUsageInfo>() {
            @Override
            public int compare(AppUsageInfo app1, AppUsageInfo app2) {
                return Long.compare(app2.getTotalTimeInForeground(), app1.getTotalTimeInForeground());
            }
        });

        return appUsageInfoList.subList(0, Math.min(limit, appUsageInfoList.size()));
    }
}

class AppUsageInfo {
    private String appName;
    private String packageName;
    private long totalTimeInForeground;

    public AppUsageInfo(String appName, String packageName, long totalTimeInForeground) {
        this.appName = appName;
        this.packageName = packageName;
        this.totalTimeInForeground = totalTimeInForeground;
    }

    public String getAppName() {
        return appName;
    }

    public String getPackageName() {
        return packageName;
    }

    public long getTotalTimeInForeground() {
        return totalTimeInForeground;
    }
}

运行结果:

img

  • 写回答

2条回答 默认 最新

  • 9102fun 2023-09-27 14:21
    关注

    我试了一下,应该是没有问题,请再检查一下权限是否获取,某些权限可能需要打开设置进行手动设置,获取了权限后重新进入页面

    img

    img

    img


    https://blog.csdn.net/qq_41009768/article/details/104360533

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月6日
  • 已采纳回答 9月28日
  • 创建了问题 9月25日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀