需求:安卓开发 获取手机中最常用的app信息。获取的结果为0(小米手机和AS自带的模拟器)
版本:安卓11
下面是我的代码
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;
}
}
运行结果: