I have build an app which Launch another app called WazeApp such that if the last active time of Waze app is greater than 5 minutes then i will launch the app else i won't.
here is method:
/**
* return true if waze was running before 5 Minutes
* @param context
* @return
*/
private static boolean wasWazeRunningBefore5Minutes(Context context) {
long current = System.currentTimeMillis();
long fiveMinuteBeforeMillis = current - TimeUnit.SECONDS.toMillis(TIME_INTERVAL);//TIME_INTERVAL=5*60 sec
UsageStatsManager usageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
List<UsageStats> stats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, fiveMinuteBeforeMillis, current);
for (UsageStats stat : stats) {
if (stat.getPackageName().equals(WAZE_PACKAGE_NAME)) {
long lastTimeUsed = stat.getLastTimeUsed();
Log.d(TAG, "last time used: " + DateFormat.format("dd/MM/yyyy, hh:mm:ss", new Date(lastTimeUsed)));
long gap = TimeUnit.MILLISECONDS.toSeconds(current - lastTimeUsed);
if (gap > TIME_INTERVAL) {
return true;
}
Log.d(TAG, "app open gap: " + gap + " sec");
break;
}
}
return false;
}
but my WazeApp is not launching for the first time i have installed it. Can Anyone fix this problem
From google examples:
https://github.com/googlesamples/android-AppUsageStatistics
And them you can use this:
http://developer.android.com/intl/es/reference/android/app/usage/UsageStats.html#getLastTimeUsed()