.jpg)
我遇到了以下方法:UserManager
public boolean isUserAGoat()用于确定进行此呼叫的用户是否受到传送的影响。
返回进行此调用的用户是否为山羊。
应该如何以及何时使用它?

网友回答:
在 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");
}
从其源开始,该方法用于返回,直到它在 API 21 中更改。false
/**
* 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;
}
看起来该方法对我们开发人员没有真正的用处。之前有人说这可能是一个复活节彩蛋。
在 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指出,由于这会破坏对调试标志的支持的特定原因,即基本上是这个用例(h / t @auselen),不会触发“无法访问的代码”。(例如)。if (false)static final boolean DEBUG = false;
我替换了 ,产生了一个更晦涩的用例。我相信你可以用这种行为来绊倒你的IDE,比如Eclipse,但是这个编辑是未来4年的事情,我没有Eclipse环境可以玩。whileif

网友回答:
这似乎是谷歌的一个内部笑话。它也出现在谷歌浏览器任务管理器中。它没有任何目的,除了一些工程师觉得它很有趣。如果你愿意的话,这本身就是一个目的。
Goats Teleported甚至还有一个巨大的铬虫报告,关于太多的传送山羊。

以下 Chromium 源代码片段是从 HN 注释中窃取的。
int TaskManagerModel::GetGoatsTeleported(int index) const {
int seed = goat_salt_ * (index + 1);
return (seed >> 16) & 255;
}
模板简介:该模板名称为【Android UserManager.isUserAGoat()的正确用例?】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【Java】栏目查找您需要的精美模板。