2017-08-13 91 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

相关问题