我打算通过检测 Playstore 安装的应用程序是否是游戏来制作一个 android 应用程序来控制我的手机使用。所以如果安装的应用程序是游戏应用程序,我的应用程序会检测到安装的应用程序是一种游戏,并且不允许游戏应用程序运行。
我想知道是否有任何源代码。
自 API 级别 21 以来,有一种方法可以检查这一点,并且最近在 API 级别 26 中进行了更改。这些是执行此操作的正确向后兼容方法。
爪哇:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
科特林:
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
}
}
Run Code Online (Sandbox Code Playgroud)
来源:Android 文档
| 归档时间: |
|
| 查看次数: |
2195 次 |
| 最近记录: |