繁体   English   中英

Android应用程序的前景用法

[英]Foreground usage for an Android app

我有一个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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM