I have started learning android very recently and I am writing a small app which just shows a toast message to a user when user opens any app. I tried googling and reading through the doccs but could not find any way to get that information. Later I found that we can get foreground app info using something called getSystemService() so I wrote this
val runningProcessInfo = mutableListOf<String>()
val appService : ActivityManager = getSystemService(ACTIVITY_SERVICE) as ActivityManager
val tasks = appService.runningAppProcesses
for(task in tasks){
if (task.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
{
runningProcessInfo.add(task.processName+"\n")
}
Toast.makeText(this.applicationContext, runningProcessInfo.toString(), Toast.LENGTH_LONG).show()
}
The second problem is how do I make apps launching in the foreground notify my app .. is there any event which I can subscribe to ? I have tried googling this too but no serious leads there .. Can someone please guide me in a right direction ? thanks :)
from How do I know when user launches a third party app in android - kotlin?
No comments:
Post a Comment