I am a newbie to android. I am developing an app locker that uses face id instead of normal pin/pattern using kotlin. I have got the list of installed apps in the system. But, how to get the information about which app is opened using service Please help.
Asked
Active
Viewed 694 times
1 Answers
0
Like this:
Java version
private String retriveAppInForeground() {
String currentApp = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
UsageStatsManager usm = (UsageStatsManager) this.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = null;
if (usm != null) {
appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
}
if (appList != null && !appList.isEmpty()) {
SortedMap<Long, UsageStats> sortedMap = new TreeMap<>();
for (UsageStats usageStats : appList) {
sortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (!sortedMap.isEmpty()) {
currentApp = sortedMap.get(sortedMap.lastKey()).getPackageName();
}
}
} else {
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (am != null) {
currentApp =(am.getRunningTasks(1).get(0)).topActivity.getPackageName();
}
}
Log.e("ActivityTAG", "Application in foreground: " + currentApp);
return currentApp;
}
Kotlin version
private fun retriveAppInForeground(): String? {
var currentApp: String? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
val usm = this.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val time = System.currentTimeMillis()
val appList: List<UsageStats>?
appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time)
if (appList != null && appList.isNotEmpty()) {
val sortedMap = TreeMap<Long, UsageStats>()
for (usageStats in appList) {
sortedMap.put(usageStats.lastTimeUsed, usageStats)
}
currentApp = sortedMap.takeIf { it.isNotEmpty() }?.lastEntry()?.value?.packageName
}
} else {
val am = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
@Suppress("DEPRECATION") //The deprecated method is used for devices running an API lower than LOLLIPOP
currentApp = am.getRunningTasks(1)[0].topActivity.packageName
}
Log.e("ActivityTAG", "Application in foreground: " + currentApp)
return currentApp
}
Make sure your app has the proper permissions for accessing the usage stats:
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>
And that the user grants the proper permission, you can take the user to the proper settings screen to enable the permission (when setting up your app):
startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
And one more thing, always make sure you search for any similar question before posting a new one.

Hugo Allexis Cardona
- 1,382
- 19
- 25
-
Can you explain this piece of code, SortedMap
mySortedMap = new TreeMap<>(); . An error pops up when i convert this to kotlin. – Faizan Ali Mar 28 '19 at 05:29 -
The TreeMap is for easier keeping entries order and accessing the latest one. What exact error is popping up? I just added the Kotlin version for your reference... You can also make that function a static one inside a PackageUtil class, for example. – Hugo Allexis Cardona Mar 29 '19 at 00:04
-
this is a great answer, but for me Kotlin code won't work for me. It can't find getSystemService function. So, it just won't run. I have the latest Android Studio and up-to-date. I simply want to display only running apps on the phone. – ThN Mar 17 '22 at 18:21