我正在尝试编写一个 android 应用程序,它会告诉我已安装应用程序的使用情况。目前我只使用当天。如何使用 queryUsageStats 获取应用程序的使用历史记录?还有其他方法吗?有人可以提供一个如何使用它的例子吗?提前致谢。
问问题
114 次
1 回答
0
抱歉迟了回应。是的,我正在谈论https://developer.android.com/reference/android/app/usage/UsageStatsManager#queryUsageStats(int,%20long,%20long)。这是我的适配器,我在其中显示当天的使用情况(仅今天)。单击回收站视图对象后,我想显示每周/每月的使用情况(在此期间的每一天)。
公共类 AppListAdapter 扩展 RecyclerView.Adapter<AppListAdapter.ListViewHolder> { private ArrayList topAppsList; 私有上下文上下文;
AppListAdapter(ArrayList<HashMap> al) {
this.topAppsList = al;
}
class ListViewHolder extends RecyclerView.ViewHolder {
TextView appName, usageTime;
ImageView appLogo;
ListViewHolder(@NonNull View itemView) {
super(itemView);
appName = itemView.findViewById(R.id.appName);
usageTime = itemView.findViewById(R.id.appUsageTime);
appLogo = itemView.findViewById(R.id.appLogo);
}
}
@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View itemView;
this.context = viewGroup.getContext();
{
itemView = LayoutInflater.from(this.context)
.inflate(R.layout.usage_app_list_item, viewGroup, false);
}
return new ListViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull final ListViewHolder listViewHolder, int i) {
HashMap<String, Long> hm = this.topAppsList.get(i);
HashMap.Entry<String, Long> entry = hm.entrySet().iterator().next();
final long usageTime = entry.getValue();
final String packageName = entry.getKey();
PackageManager pm = this.context.getPackageManager();
ApplicationInfo applicationInfo;
try {
applicationInfo = pm.getApplicationInfo(packageName, 0);
} catch (final PackageManager.NameNotFoundException e) {
applicationInfo = null;
}
final String applicationName = (String) (applicationInfo != null ? pm.getApplicationLabel(applicationInfo) : packageName);
final Drawable appIcon = applicationInfo != null ? pm.getApplicationIcon(applicationInfo) : null;
if (appIcon != null && listViewHolder.appLogo != null) {
listViewHolder.appLogo.setImageDrawable(appIcon);
}
if (listViewHolder.usageTime != null) {
long hours = TimeUnit.MILLISECONDS.toHours(usageTime);
long minutes = TimeUnit.MILLISECONDS.toMinutes(usageTime - TimeUnit.HOURS.toMillis(hours));
if (hours != 0) {
listViewHolder.usageTime.setText(String.format(Locale.getDefault(), "%dh %02dmin", (int) hours, (int) minutes));
} else if (minutes != 0) {
listViewHolder.usageTime.setText(String.format(Locale.getDefault(), "%dmin", (int) minutes));
} else {
listViewHolder.usageTime.setText(String.format(Locale.getDefault(), "Less than %dmin", 1));
}
}
if (listViewHolder.usageTime != null) {
listViewHolder.appName.setText(applicationName);
}
listViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
// @SuppressLint("SetTextI18n")
@Override
public void onClick(View view) {
Intent intent = new Intent(context, AppWeeklyUsageActivity.class);
intent.putExtra("app_name",applicationName);
long usageTimeHours = TimeUnit.MILLISECONDS.toHours(usageTime);
long usageTimeMinutes = TimeUnit.MILLISECONDS.toMinutes(usageTime - TimeUnit.HOURS.toMillis(usageTimeHours));
String usageTimeStringH = Long.toString(usageTimeHours);
String usageTimeStringM = Long.toString(usageTimeMinutes);
if (usageTimeHours < 1) {
intent.putExtra("usageTimeM", usageTimeStringM);
} else {
intent.putExtra("usageTimeH", usageTimeStringH);
intent.putExtra("usageTimeM", usageTimeStringM);
}
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return topAppsList.size();
}
}
于 2020-11-28T11:41:31.023 回答