2017-02-28 41 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() 

此方法返回的時間,而不是有多少 等創造的主要活動 的文件的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 
} 
+0

非常感謝埃利亞斯。 – user7192115

相關問題