[英]Foreground usage for an Android app
I have an Android app that tracks last time an app used in the foreground. 我有一个Android应用程序,该应用程序跟踪上一次在前台使用的应用程序。 How can I count each time the app is active for an interval of time? 在一段时间内每次激活该应用程序时,我该如何计数?
public static void printUsageStats(List<UsageStats> usageStatsList){
for (UsageStats u : usageStatsList){
Log.d(TAG, "Pkg: " + u.getPackageName() + "\t" + "ForegroundTime: "
+ u.getTotalTimeInForeground()) ;
}
}
log to a file! 登录到文件!
getTotalTimeInForeground()
this method returns times and not how MANY so create file onCreate() of main activity and check if file exists and set new value 此方法返回时间,而不是如何返回,因此如何在主要活动中创建文件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.