想要改进这篇文章?提供这个问题的详细答案,包括引文和解释为什么你的答案是正确的。没有足够细节的答案可能会被编辑或删除。
我正在查看 Android 4.2 中引入的新 API。在查看 UserManager
类时,我遇到了以下方法:
public boolean isUserAGoat() 用于确定进行此呼叫的用户是否受到传送。返回进行此调用的用户是否是山羊。
应该如何以及何时使用它?
Android R 更新:
在 Android R 中,此方法始终返回 false。谷歌表示这样做是为了“保护山羊隐私”:
/**
* Used to determine whether the user making this call is subject to
* teleportations.
*
* <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
* now automatically identify goats using advanced goat recognition technology.</p>
*
* <p>As of {@link android.os.Build.VERSION_CODES#R}, this method always returns
* {@code false} in order to protect goat privacy.</p>
*
* @return Returns whether the user making this call is a goat.
*/
public boolean isUserAGoat() {
if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R) {
return false;
}
return mContext.getPackageManager()
.isPackageAvailable("com.coffeestainstudios.goatsimulator");
}
上一个答案:
从他们的 source 开始,该方法用于返回 false
,直到它在 API 21 中被更改。
/**
* Used to determine whether the user making this call is subject to
* teleportations.
* @return whether the user making this call is a goat
*/
public boolean isUserAGoat() {
return false;
}
看起来该方法对我们作为开发人员没有真正的用途。之前有人说过它可能是一个Easter egg。
在 API 21 中,实施已更改为检查是否已安装应用程序包 com.coffeestainstudios.goatsimulator
/**
* Used to determine whether the user making this call is subject to
* teleportations.
*
* <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
* now automatically identify goats using advanced goat recognition technology.</p>
*
* @return Returns true if the user making this call is a goat.
*/
public boolean isUserAGoat() {
return mContext.getPackageManager()
.isPackageAvailable("com.coffeestainstudios.goatsimulator");
}
我不知道这是否是“官方”用例,但以下会在 Java 中产生警告(如果与 return
语句混合,可能会进一步产生编译错误,导致代码无法访问):
while (1 == 2) { // Note that "if" is treated differently
System.out.println("Unreachable code");
}
但是这是合法的:
while (isUserAGoat()) {
System.out.println("Unreachable but determined at runtime, not at compile time");
}
因此,我经常发现自己编写了一个愚蠢的实用程序方法,以最快的方式模拟出一个代码块,然后在完成调试时找到对它的所有调用,因此只要实现不改变,就可以使用它。
JLS 指出 if (false)
不会触发“无法访问的代码”,具体原因是这会破坏对调试标志的支持,即基本上是这个用例 (h/t @auselen)。 (例如 static final boolean DEBUG = false;
)。
我将 while
替换为 if
,从而产生了一个更加模糊的用例。我相信你可能会因为这种行为而绊倒你的 IDE,比如 Eclipse,但是这个编辑是 4 年后的未来,我没有 Eclipse 环境可以玩。
else
块等,它将导致编译错误。
isUserAGoat()
将返回 true。祝你好运,当你的代码突然开始在“随机”设备上出现奇怪的行为时!
这似乎是谷歌的一个内部笑话。它也出现在 Google Chrome 任务管理器中。它没有任何目的,只是一些工程师觉得它很有趣。如果您愿意,这本身就是一个目的。
在 Chrome 中,使用 Shift+Esc 打开任务管理器。右键单击以添加 Goats Teleported 列。想知道。
甚至还有一个关于 too many teleported goats 的巨大 Chromium 错误报告。
https://i.stack.imgur.com/1nwdC.png
以下 Chromium source code snippet 是从 HN 评论中窃取的。
int TaskManagerModel::GetGoatsTeleported(int index) const {
int seed = goat_salt_ * (index + 1);
return (seed >> 16) & 255;
}
作为对@djechlin answer 的补充(顺便说一句,答案很好!),当您想在某些特定迭代中停止或一个特定的递归调用,例如:
https://i.stack.imgur.com/mW1Lf.png
可以使用 isUserAGoat()
代替将在 IDE 中显示为警告的虚拟变量声明,并且在 Eclipse 特定情况下,会阻塞断点标记,从而难以启用/禁用它。如果将该方法用作约定,则所有调用稍后都可以通过某些脚本过滤(可能在提交阶段?)。
https://i.stack.imgur.com/MFIBa.png
谷歌的家伙是 Eclipse 的重度用户(他们提供了几个项目作为 Eclipse 插件:Android SDK、GAE 等),所以@djechlin 的答案和这个互补的答案很有意义(至少对我来说)。
在每个版本的 Android 中都有一个有趣的命名方法/常量/任何东西。
我见过的唯一实际用途是在 Last Call for Google I/O 竞赛中,他们询问特定版本的内容,以查看参赛者是否阅读了每个版本的 API 差异报告。比赛也有编程问题,但通常一些琐事可以首先自动评分,以将提交的数量降低到更容易检查的合理数量。
在语音识别学科中,用户分为山羊和绵羊。
例如,这里 on page 89:
绵羊是语音识别工作异常出色的人,山羊是语音识别工作异常糟糕的人。只有语音识别器知道它们之间的区别。人们无法预测谁的声音会被轻松识别,谁的声音不会。最好的策略是设计界面,使其能够处理各种环境中的各种声音
也许,未来计划将Android用户标记为山羊,以便能够根据山羊的需求配置语音识别引擎。 ;-)
As of API 21 (the first Android 5.0/Lollipop SDK),这会检测是否安装了 Goat Simulator 应用程序:
/**
* Used to determine whether the user making this call is subject to
* teleportations.
*
* <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
* now automatically identify goats using advanced goat recognition technology.</p>
*
* @return Returns true if the user making this call is a goat.
*/
public boolean isUserAGoat() {
return mContext.getPackageManager()
.isPackageAvailable("com.coffeestainstudios.goatsimulator");
}
这应该清楚地表明 djechlin's suggestion 将其用作无警告 if (false)
是一种潜在的灾难性策略。以前为每个设备返回的 false
现在返回一个看似随机的值:如果它在您的代码中埋得足够深,则可能需要 很长时间 时间才能确定新错误的来源。
底线:如果您不控制方法的实现并决定将其用于 API 文档中未说明的目的,那么您将遇到麻烦。
if(false)
投入生产?来这里。如果这就是您的代码的样子,那么此实现更改是您的问题中的最少。
Google 非常喜欢山羊和基于山羊的Easter eggs。甚至还有previous Stack Overflow posts about it。
正如之前的帖子中提到的,它也存在于 Chrome 任务管理器 (it first appeared in the wild in 2009) 中:
<message name="IDS_TASK_MANAGER_GOATS_TELEPORTED_COLUMN" desc="The goats teleported column">
Goats Teleported
</message>
And then in Windows, Linux and Mac versions of Chrome early 2010)。 “Goats Teleported”的数量实际上是 random:
int TaskManagerModel::GetGoatsTeleported(int index) const {
int seed = goat_salt_ * (index + 1);
return (seed >> 16) & 255;
}
其他谷歌对山羊的引用包括:
用山羊割草
山羊是 baaaahk
据我所知,goats 和 Google 最早的关联属于原始的“Mowing with goats”博客文章。
我们可以放心地假设它只是一个复活节彩蛋,除了返回 false
外,没有任何实际用途。
有一个类似的调用 isUserAMonkey()
,如果正在使用 MonkeyRunner tool,则返回 true。 SDK 的解释和这个一样好奇。
public static boolean isUserAMonkey(){} 如果用户界面当前正被猴子弄乱,则返回 true。
Here 是来源。
我希望这是在预期新的 SDK 工具时添加的,该工具名为 something with a goat,并且实际上可以用于测试该工具的存在。
另请参阅类似的问题 Strange function in ActivityManager: isUserAMonkey. What does this mean, what is its use?。
ActivityManager
报告它是否已被 monkey
ui 压力测试工具调用,但我从未证实这一点。
有趣的复活节彩蛋。在 Ubuntu 版本的 Chrome 中,在任务管理器 (shift+esc) 中,通过右键单击可以添加一个在意大利语版本中为“Capre Teletrasportate”(Teleported Goats)的科幻专栏。
关于它的一个有趣的理论是here。
这不是一个内部笑话
显然它只是 Goat Simulator - by Coffee Stain Studios 的应用程序检查器
如果您安装了山羊模拟器,那么您就是山羊。如果你没有安装它,你就不是山羊。
我想这更像是一位开发人员的个人实验,最有可能找到有共同兴趣的人。
isUserAGoat
文档也提到了山羊传送。所以这绝对是一个内部笑话。
从可用性和可访问性的角度来看,了解您的用户是否是山羊很重要。
如果山羊要真正从信息时代所提供的所有东西中受益,它们的需求实际上与他们的软件有很大不同。
我认为有些善良的开发人员关心我们所有的用户,而不仅仅是时尚的用户,这真是太好了。
不定期副业成功案例分享
false
。