Advertisement
reset_man

котлин: как отслеживать запущенные приложения и определять, является ли запущенное приложение игрой:

May 4th, 2024
1,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.54 KB | None | 0 0
  1. import android.app.ActivityManager
  2. import android.content.Context
  3. import android.content.pm.ApplicationInfo
  4.  
  5. fun isGame(context: Context, packageName: String): Boolean {
  6.     val packageManager = context.packageManager
  7.     val applicationInfo: ApplicationInfo = try {
  8.         packageManager.getApplicationInfo(packageName, 0)
  9.     } catch (e: Exception) {
  10.         return false
  11.     }
  12.  
  13.     return applicationInfo.flags and ApplicationInfo.FLAG_IS_GAME != 0
  14. }
  15.  
  16. fun getRunningApps(context: Context): List<String> {
  17.     val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
  18.     val runningTasks = activityManager.runningAppProcesses ?: emptyList()
  19.     val runningApps = mutableListOf<String>()
  20.  
  21.     for (taskInfo in runningTasks) {
  22.         val packageName = taskInfo.processName
  23.         runningApps.add(packageName)
  24.     }
  25.  
  26.     return runningApps
  27. }
  28.  
  29. fun main() {
  30.     val context: Context = getContext() // Предполагается, что у вас уже есть доступ к контексту приложения
  31.     val runningApps = getRunningApps(context)
  32.  
  33.     for (app in runningApps) {
  34.         val isGameApp = isGame(context, app)
  35.         if (isGameApp) {
  36.             println("$app - это игровое приложение")
  37.         } else {
  38.             println("$app - это не игровое приложение")
  39.         }
  40.     }
  41. }
  42.  
  43. // Функция getContext() осталась невыданной, так как она зависит от конкретного типа приложения и его структуры
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement