Reputation: 347
I am developing a utility based android application and need to know the last used time of another applications. I am able to manage the information using UsageStatsManager and its works fine.
https://developer.android.com/reference/android/app/usage/UsageStatsManager.html
But the problem is, it was added in API 21 and for the old version it does not work.
That's why I need another solution to get the informations for the API level before 21 i.e API 8/9 - 20. Is there any different solution to get the information like any hidden log file where the information stored by the system or something else.
Upvotes: 3
Views: 2240
Reputation: 548
For API level 21 and lower you could try getRecentTasks method. It's not recommended to be used for core logic in an application, so use it on your own risk.
Upvotes: 0
Reputation: 28238
I'm not familiar with any API's for Android < 21, but I see one very easy way.
This involves you saving the time.
First, you need to get the time:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy");
String currentDateandTime = sdf.format(new Date());,
Then you can save the String.
That I leave up to you. You can use Shared Prefs, SQLite, internal storage or external storage.
As for parsing the date:
SimpleDateFormat format = new SimpleDateFormat("HH:mm dd/MM/yyyy");
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
And check this answer for checking if an app has been closed.
On API < 21 you have to piece together a lot of functionality to get the functionality you are asking for. You also have tto check when an app is opened and when it then is closed. When the app then is closed, you save the date.
Now you can on Android < 21 check when an app was last used.
I recommend you use this on Android < 21 and use the UsageStatsManager on Api >= 21. The way I propose requires the usage of a Service which can use a lot of power. The API levels where you need this way and not the UsageStatsManager slowly decreasing in numbers. Eventually you will be able to use the UsageStatsManager exclusively.
Upvotes: 1