Android应用程序的前景用法



我有一个Android应用程序,该应用程序上一次在前景中使用的应用程序。我如何计算每个应用程序在时间间隔中处于活动状态时?

public static void printUsageStats(List<UsageStats> usageStatsList){
    for (UsageStats u : usageStatsList){
        Log.d(TAG, "Pkg: " + u.getPackageName() +  "t" + "ForegroundTime: "
                + u.getTotalTimeInForeground()) ;
    }
}

登录到文件!

getTotalTimeInForeground()

此方法返回时间,而不是多少因此,创建主要活动的文件oncreate()并检查文件是否存在并设置新值

File f = getApplicationContext().getFileStreamPath("count");
if(!f.exists){
try {
String fileName = ".countFile";
FileOutputStream fOut = context.openFileOutput(fileName, MODE_PRIVATE);//don't use append
fOut.write((/*Count Integer*/).getBytes());
fOut.flush();
fOut.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
else{
//read file and add +1 to existing file
}

最新更新