2017-08-13 92 views
-1

我想製作一個應用程序,它可以計算我在一天中訪問應用程序的時間,並且每天應該重置共享偏好。即它應該每天從0開始自動計數。我如何使用共享首選項來執行此操作。以下是代碼:我想統計每天打開應用程序的次數

prefs = getPreferences(Context.MODE_PRIVATE); 
    editor = prefs.edit(); 
    // editor.clear(); 
    totalCount = prefs.getInt("counter", 0); 
    totalCount++; 
    editor.putInt("counter", totalCount); 
    editor.commit(); 

Toast.makeText(getApplicationContext(),"Hello User..!! You have entered the App " +totalCount+ " time today",Toast.LENGTH_LONG).show(); 
+0

而不是存儲爲**計數器**,可以使用作爲**「[某種格式的日期] - 計數器」**在這你可以得到每一天 –

+0

我該怎麼做。你可以告訴嗎? @MuthukrishnanRajendran – Abhi

回答

0

你可以這樣試試,這樣就可以跟蹤每一天,我們也可以得到過去的日子。

String formattedDate = new SimpleDateFormat("ddMMMyyyy").format(new Date()); 
String counterKey = formattedDate + "-counter"; 

prefs = getPreferences(Context.MODE_PRIVATE); 
editor = prefs.edit(); 
// editor.clear(); 
totalCount = prefs.getInt(counterKey, 0); 
totalCount++; 
editor.putInt(counterKey, totalCount); 
editor.commit(); 

但你也可以嘗試SQLite數據庫。以存儲和檢索多天。

+0

This Worked ..!我可以在活動的列表視圖中顯示天數嗎? @MuthukrishnanRajendran – Abhi

相關問題