2017-02-28 18 views
0

앱이 전경에서 마지막으로 사용 된 시간을 추적하는 Android 앱이 있습니다. 앱이 일정 시간 동안 활성화 될 때마다 어떻게 계산합니까?Android 앱의 전경 사용

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

} 

답변

0

로그에 저장

getTotalTimeInForeground() 

이 방법은 시간이 아니라 얼마나 많은 그래서 새로운 가치

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 
} 
+0

정말 감사합니다 엘리야의 주요 활동 의 파일에서 onCreate()를 만들고 파일이 존재하는지 확인하고 설정을 반환합니다. – user7192115