1

I am planning to make an android app for controlling my phone usage by detecting if an playstore-installed app is game or not. So if the installed app is a game app, my app would detect that the installed app is a kind of game and would not allow the game app to run.

I wonder if there is any source code for this.

Tarod
  • 6,732
  • 5
  • 44
  • 50
user5150273
  • 195
  • 1
  • 2
  • 8

1 Answers1

7

There is a way to check this since API level 21 and it has recently changed in API level 26. These are the correct backwards compatible ways to do this.

Java:

public static boolean packageIsGame(Context context, String packageName) {
    try {
        ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName, 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return info.category == ApplicationInfo.CATEGORY_GAME;
        } else {
            // We are suppressing deprecation since there are no other options in this API Level
            //noinspection deprecation
            return (info.flags & ApplicationInfo.FLAG_IS_GAME) == ApplicationInfo.FLAG_IS_GAME;
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.e("Util", "Package info not found for name: " + packageName, e);
        // Or throw an exception if you want
        return false;
    }
}

Kotlin:

fun packageIsGame(context: Context, packageName: String): Boolean {
    return try {
        val info: ApplicationInfo = context.packageManager.getApplicationInfo(packageName, 0)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            info.category == ApplicationInfo.CATEGORY_GAME
        } else {
            // We are suppressing deprecation since there are no other options in this API Level
            @Suppress("DEPRECATION")
            (info.flags and ApplicationInfo.FLAG_IS_GAME) == ApplicationInfo.FLAG_IS_GAME
        }
    } catch (e: PackageManager.NameNotFoundException) {
        Log.e("Util", "Package info not found for name: " + packageName, e)
        // Or throw an exception if you want
        false
    }
}

Source: Android Documentation

Roberto Anić Banić
  • 1,411
  • 10
  • 21