如何以编程方式检查该应用是否为游戏? [英] How to check if the app is game or not programmatically?

查看:137
本文介绍了如何以编程方式检查该应用是否为游戏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算通过检测Playstore安装的应用程序是否为游戏来制作一个可控制我的手机使用率的android应用程序.因此,如果安装的应用程序是游戏应用程序,则我的应用程序将检测到安装的应用程序是一种游戏,并且将不允许该游戏应用程序运行.

我想知道是否有任何源代码.

解决方案

自API级别21以来,有一种方法可以对此进行检查,并且最近已在API级别26中进行了更改.这是正确的向后兼容方法./p>

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;
    }
}
 

科特琳:

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
    }
}

来源:Android文档

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.

解决方案

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

这篇关于如何以编程方式检查该应用是否为游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆