简体   繁体   English

Android应用程序的前景用法

[英]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.

相关问题 Android应用程序自动进入前台 - Android app automatically coming to foreground Android-检测应用移至背景和前景 - Android - Detect app moving to background and foreground Android,前台服务能否使应用程序继续运行? - Android, does foreground service keep app alive? 仅在应用程序处于前台时才收到Android通知 - Android notifications only received if app is in foreground Android应用中的云端硬盘使用情况 - cloud drive usage in Android App Android-服务应用程序如何将KeyEvent调度到另一个前景应用程序 - Android - How can a Service App dispatch KeyEvents to another Foreground App Android Java:当应用程序不在前台时如何完全删除ScheduledExecutorService实例 - Android Java : How to completely remove instance of ScheduledExecutorService when App is not in foreground 如何获取运行前台的Android应用的Activity实例? - How to get the Activity instance of an Android app running the foreground? 应用程序处于前台状态时锁定屏幕会阻止后台执行(Android) - Locking screen while app is in foreground prevents background execution (Android) 状态栏通知单击事件Android的后台前景应用程序状态 - Status bar notification click event Android for background foreground app state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM