Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何正确使用queryUsageStats_Java_Android_Usagestatsmanager - Fatal编程技术网

Java 如何正确使用queryUsageStats

Java 如何正确使用queryUsageStats,java,android,usagestatsmanager,Java,Android,Usagestatsmanager,我正在尝试编写一个android应用程序,它将告诉我已安装应用程序的使用情况。目前我只有当天的用法。如何使用queryUsageStats获取应用程序的使用历史?还有别的办法吗?有人能举例说明如何使用它吗? 提前感谢。很抱歉回复太晚。 是的,我是说。 这是我的适配器,我在其中显示今天的使用情况(仅今天)。单击“回收者视图”对象后,我希望显示每周/每月使用量(此期间每天的使用量) 公共类AppListAdapter扩展了RecyclerView.Adapter{ 私人ArrayList topAp

我正在尝试编写一个android应用程序,它将告诉我已安装应用程序的使用情况。目前我只有当天的用法。如何使用queryUsageStats获取应用程序的使用历史?还有别的办法吗?有人能举例说明如何使用它吗? 提前感谢。

很抱歉回复太晚。 是的,我是说。 这是我的适配器,我在其中显示今天的使用情况(仅今天)。单击“回收者视图”对象后,我希望显示每周/每月使用量(此期间每天的使用量)

公共类AppListAdapter扩展了RecyclerView.Adapter{ 私人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();
}
AppListAdapter(ArrayList al){
this.toppslist=al;
}
类ListViewHolder扩展了RecyclerView.ViewHolder{
TextView appName,usageTime;
ImageView-appLogo;
ListViewHolder(@NonNull视图项视图){
超级(项目视图);
appName=itemView.findviewbyd(R.id.appName);
usageTime=itemView.findViewById(R.id.appUsageTime);
appLogo=itemView.findviewbyd(R.id.appLogo);
}
}
@非空
@凌驾
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup ViewGroup,int viewType){
查看项目视图;
this.context=viewGroup.getContext();
{
itemView=LayoutInflater.from(this.context)
.充气(R.layout.usage\u app\u list\u项,视图组,false);
}
返回新的ListViewHolder(itemView);
}
@凌驾
BindViewHolder上的公共无效(@NonNull final ListViewHolder ListViewHolder,int i){
HashMap hm=this.topAppsList.get(i);
HashMap.Entry=hm.entrySet().iterator().next();
final long usageTime=entry.getValue();
最后一个字符串packageName=entry.getKey();
PackageManager pm=this.context.getPackageManager();
应用信息应用信息;
试一试{
applicationInfo=pm.getApplicationInfo(packageName,0);
}捕获(最终PackageManager.NameNotFounde异常){
applicationInfo=null;
}
最后一个字符串applicationName=(字符串)(applicationInfo!=null?pm.getApplicationLabel(applicationInfo):packageName);
最终可绘制的appIcon=applicationInfo!=null?pm.getApplicationIcon(applicationInfo):null;
if(appIcon!=null&&listViewHolder.appLogo!=null){
listViewHolder.appLogo.setImageDrawable(appIcon);
}
if(listViewHolder.usageTime!=null){
长时间=时间单位。毫秒。到小时(usageTime);
long minutes=TimeUnit.millises.toMinutes(usageTime-TimeUnit.HOURS.toMillis(HOURS));
如果(小时数!=0){
listViewHolder.usageTime.setText(String.format(Locale.getDefault(),%dh%02dmin),(int)小时,(int)分钟);
}否则如果(分钟!=0){
listViewHolder.usageTime.setText(String.format(Locale.getDefault(),%dmin',(int)分钟));
}否则{
listViewHolder.usageTime.setText(String.format(Locale.getDefault(),“小于%dmin”,1));
}
}
if(listViewHolder.usageTime!=null){
listViewHolder.appName.setText(应用程序名);
}
listViewHolder.itemView.setOnClickListener(新视图.OnClickListener(){
//@SuppressLint(“SetTextI18n”)
@凌驾
公共void onClick(视图){
意向意向=新意向(上下文,AppWeeklyUsageActivity.class);
intent.putExtra(“应用程序名称”,应用程序名称);
long usageTimeHours=时间单位。毫秒。到小时(usageTime);
long usageTimeMinutes=TimeUnit.millides.toMinutes(usageTime-TimeUnit.HOURS.toMillis(usageTimeHours));
字符串usageTimeStringH=Long.toString(usageTimeHours);
字符串usageTimeStringM=Long.toString(usageTimeMinutes);
如果(usageTimeHours<1){
intent.putExtra(“usageTimeM”,usageTimeStringM);
}否则{
意图。putExtra(“usageTimeH”,usageTimeStringH);
intent.putExtra(“usageTimeM”,usageTimeStringM);
}
背景。开始触觉(意图);
}
});
}
@凌驾
public int getItemCount(){
返回topAppsList.size();
}

}

您能更具体地回答您的问题吗?“queryUsageStats”毫无意义。它只是一个变量/属性的名称。发布一些您正在处理的代码。@GBisconcini很明显,他们指的是@RyanM,那么您为什么要将链接放在引用它的位置呢?