In my application, you need to get the date and time of the all applications last launch on the device. Can I somehow get this information by standard means?
Asked
Active
Viewed 917 times
-1
-
you can get last launch app name but not last date time ,you can refer https://stackoverflow.com/questions/3290936/android-detect-when-other-apps-are-launched – Vij Jun 06 '17 at 12:09
3 Answers
0
Just save the time on every launch
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
long lastLaunchMillis = sharedPref.getLong("lastLaunch", 0);
if (lastLaunchMillis != 0) {
Calendar lastLaunch = Calendar.getInstance();
lastLaunch.setTimeInMillis(lastLaunchMillis);
//last launch found, handle
}
sharedPref.edit().putLong("lastLaunch", Calendar.getInstance().getTimeInMillis()).apply();
Edit: For launch statistics of other apps, have a look at UsageStatsManager
. You can find a sample on its usage here.

F43nd1r
- 7,690
- 3
- 24
- 62
-
Thanks, but I need to know the time of the last launch of not only my application, but also other installed ones. – user1854307 Jun 06 '17 at 12:00
-
-
The link is only for >=`lollipop` devices . What about pre-lollipop devices? – Lim CHAN Sep 14 '17 at 09:04
0
You can do that with creating an Application class and set it from your app's manifest file. Step by step:
Create a class extends
Application
:public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); // save date to sharedpref PreferenceManager.getDefaultSharedPreferences(this). edit().putLong("LAST_LAUNCH_DATE_MS", new Date().getTime()).apply(); } }
Set it to your app from manifest file:
<application android:name="com.my.project.MyApplication" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> ..... ..... </application>
You can get last launch time as milliseconds from SharedPreferences
now.

Batuhan Coşkun
- 2,961
- 2
- 31
- 48
-
That will not "get the date and time of **all** applications last launch on the device" (emphasis added). – CommonsWare Jun 06 '17 at 12:19
0
Object var10000 = this.getSystemService(USAGE_STATS_SERVICE);
UsageStatsManager usageStatsManager = (UsageStatsManager) var10000;
Log.d("TAG", "" + usageStatsManager);
List<UsageStats> queryUsageStats = usageStatsManager.queryUsageStats(INTERVAL_DAILY, currentTime - (long) 600000, currentTime);
Log.d("TAG", "" + queryUsageStats);
long last_used;
for(i=0 , i<queryUsageStats , i++){
last_used = queryUsageStats .get(i).getLastTimeUsed();
Log.d("tag", "" + queryUsageStats );
}
here is the way you can do that, you will get time in epos time so you have to convert it to normal time format.

shyam sojitra
- 53
- 6