-1

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?

user1854307
  • 580
  • 4
  • 8
  • 21
  • 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 Answers3

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
0

You can do that with creating an Application class and set it from your app's manifest file. Step by step:

  1. 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();
        }
    }
    
  2. 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
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.