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 638 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
2 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,555
- 3
- 23
- 56
-
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,923
- 2
- 28
- 47
-
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