how to properly use queryUsageStats

294 Views Asked by At

I'm trying to write an android app, which will tell me usage of my installed apps. Currently i have only usage of current day. How to get to history of app's usage with queryUsageStats? Is there another way? Could someone provide an example of how to use it? Thanks in advance.

1

There are 1 best solutions below

0
On

Sorry for late response. Yes, I am talking about to https://developer.android.com/reference/android/app/usage/UsageStatsManager#queryUsageStats(int,%20long,%20long). Here is my Adapter in which I display usage of this day (only today). After clicking a recycler view object i would like to display weekly/monthly usage (of every day in this period).

public class AppListAdapter extends RecyclerView.Adapter<AppListAdapter.ListViewHolder> { private ArrayList topAppsList; private Context context;

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();
}

}