3

I would like to know when an app (i.e., Facebook or WhatsApp) was last opened.

Without root, is there any way to programmatically determine when an app was last opened in Android?

Important Info:

One issue I have regarding previous suggestions is, if I keep some variation of log then I wouldn't be able to determine when an app was last used if it was used before my Android application was installed.

Md. Sabbir Ahmed
  • 850
  • 8
  • 22
fraruphe
  • 31
  • 1
  • 4
  • keep a logfile that is updated when opening-closing the app – Stultuske Mar 22 '16 at 14:16
  • 2
    save app opening time in to preference and updated it next time , now for the very first time you start your application , check that preference value and set your logic as you want !!!!! – Radhey Mar 22 '16 at 14:25
  • Note that the full system log is only visible if you have root. – Al Lelopath Mar 22 '16 at 14:27
  • 1
    @Radhe He means ANY app, not his app. – Bevor Mar 22 '16 at 14:35
  • sorry @Bevor , he didn't mention his app or every app , so gave suggestion . anyway even I want to know for every app ,share solution if you get #fraruphe. – Radhey Mar 23 '16 at 05:34

1 Answers1

2

From google examples:

https://github.com/googlesamples/android-AppUsageStatistics

public List<UsageStats> getUsageStatistics(int intervalType) {
        // Get the app statistics since one year ago from the current time.
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.YEAR, -1);

        List<UsageStats> queryUsageStats = mUsageStatsManager
                .queryUsageStats(intervalType, cal.getTimeInMillis(),
                        System.currentTimeMillis());

        if (queryUsageStats.size() == 0) {
            Log.i(TAG, "The user may not allow the access to apps usage. ");
            Toast.makeText(getActivity(),
                    getString(R.string.explanation_access_to_appusage_is_not_enabled),
                    Toast.LENGTH_LONG).show();
            mOpenUsageSettingButton.setVisibility(View.VISIBLE);
            mOpenUsageSettingButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
                }
            });
        }
        return queryUsageStats;
    }

And them you can use this:

http://developer.android.com/intl/es/reference/android/app/usage/UsageStats.html#getLastTimeUsed()

if( mUsageStats.getPackageName().equals("YOUR PACKAGE NAME")){
    mUsageStats.getLastTimeUsed();
}
Miguel Benitez
  • 2,322
  • 10
  • 22