我正在查看Android 4.2中引入的新API。在查看UserManager类时,我遇到了以下方法:
公共布尔值isUserAGoat()用于确定拨打此呼叫的用户是否需要进行远程传送。返回进行此调用的用户是否为山羊。
该如何使用以及何时使用?
我正在查看Android 4.2中引入的新API。在查看UserManager类时,我遇到了以下方法:
公共布尔值isUserAGoat()用于确定拨打此呼叫的用户是否需要进行远程传送。返回进行此调用的用户是否为山羊。
该如何使用以及何时使用?
当前回答
这似乎是谷歌内部的一个笑话。谷歌Chrome任务管理器中也有此功能。除了一些工程师觉得它很有趣之外,它没有任何用途。这本身就是一个目的,如果你愿意的话。
在Chrome中,用Shift+Esc键打开任务管理器。右键单击以添加Goats Teleported列。想知道
甚至有一个巨大的铬虫报告说,有太多的被传送的山羊。
以下Chromium源代码片段是从HN注释中窃取的。
int TaskManagerModel::GetGoatsTeleported(int index) const {
int seed = goat_salt_ * (index + 1);
return (seed >> 16) & 255;
}
其他回答
这不是内部笑话
显然它只是一个应用程序检查器山羊模拟器-咖啡色工作室
如果你安装了山羊模拟器,你就是一只山羊。如果你没有安装,你就不是山羊。
我想这更多是一个开发者的个人实验,最有可能找到有共同兴趣的人。
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");
}
上一个答案:
从他们的源代码来看,该方法在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");
}
这是来源和变化。
这似乎是谷歌内部的一个笑话。谷歌Chrome任务管理器中也有此功能。除了一些工程师觉得它很有趣之外,它没有任何用途。这本身就是一个目的,如果你愿意的话。
在Chrome中,用Shift+Esc键打开任务管理器。右键单击以添加Goats Teleported列。想知道
甚至有一个巨大的铬虫报告说,有太多的被传送的山羊。
以下Chromium源代码片段是从HN注释中窃取的。
int TaskManagerModel::GetGoatsTeleported(int index) const {
int seed = goat_salt_ * (index + 1);
return (seed >> 16) & 255;
}
有趣的复活节彩蛋。在Ubuntu版本的Chrome中,在任务管理器(shift+esc)中,右键单击可以添加一个科幻专栏,在意大利语版本中为“Capre Teletransportate”(Teleported Goats)。
这里有一个有趣的理论。
我不知道这是否是“官方”用例,但以下内容在Java中产生了一个警告(如果与返回语句混合,会进一步产生编译错误,导致无法访问代码):
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指出,如果(false)没有触发“不可访问的代码”,因为这会破坏对调试标志的支持,即基本上是这个用例(h/t@auselen)。(例如,静态最终布尔值DEBUG=false)。
我用if替换了while,产生了一个更模糊的用例。我相信你可以像Eclipse一样,用这种行为来破坏你的IDE,但这种编辑是在4年后的将来,我没有一个Eclipse环境可以玩。