我希望我的代码在模拟器上运行时与在设备上运行时略有不同。(例如,使用10.0.2.2代替公共URL在开发服务器上自动运行。)检测Android应用程序何时在模拟器中运行的最佳方法是什么?


当前回答

if ("sdk".equals( Build.PRODUCT )) {
 // Then you are running the app on the emulator.
        Log.w("MyAPP", "\n\n  Emulator \n\n"); 
}

其他回答

if (Build.BRAND.equalsIgnoreCase("generic")) {
    // Is the emulator
}

所有BUILD引用都是BUILD。道具值,所以你必须考虑到如果你要把这个放到发布代码中,你可能会有一些根用户因为某种原因修改了他们的。实际上没有任何修改需要使用generic作为品牌,除非特别尝试模拟模拟器。

指纹是构建编译和内核编译签名。有一些构建使用通用的,通常直接来自谷歌。

在修改过的设备上,IMEI也有可能被归零,所以这是不可靠的,除非您完全阻止修改过的设备。

金鱼是所有其他设备扩展的基础android构建。每个Android设备都有一个init.金鱼.rc,除非被黑客攻击并出于未知原因删除。

以下是我的解决方案(它只适用于在调试机器上运行web服务器): 我已经创建了一个后台任务,当应用程序启动时启动。它查找http://10.0.2.2,如果它存在,它将全局参数(IsDebug)更改为true。这是一种无声的方式来找出你在哪里跑步。

public class CheckDebugModeTask extends AsyncTask<String, Void, String> {
public static boolean IsDebug = false;

public CheckDebugModeTask()
{

}

@Override
protected String doInBackground(String... params) {     
  try {
    HttpParams httpParameters = new BasicHttpParams();
    int timeoutConnection = 1000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    int timeoutSocket = 2000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    String url2 = "http://10.0.2.2";        
          HttpGet httpGet = new HttpGet(url2);
    DefaultHttpClient client = new DefaultHttpClient(httpParameters);

    HttpResponse response2 = client.execute(httpGet);
    if (response2 == null || response2.getEntity() == null || response2.getEntity().getContent() == null)
    return "";

    return "Debug";

} catch (Exception e) {
    return "";
}
}

@Override
protected void onPostExecute (String result)
{       
if (result == "Debug")
{
    CheckDebugModeTask.IsDebug = true;
}
}

从主活动onCreate:

CheckDebugModeTask checkDebugMode = new CheckDebugModeTask();
checkDebugMode.execute("");

一个常见的是Build.FINGERPRINT.contains("generic")

安卓id不适合我,我现在用的是:

"google_sdk".equals( Build.PRODUCT );

试试这个方法。

在谷歌和Genymotion模拟器上测试。

public Boolean IsVM() {
    String radioVersion = android.os.Build.getRadioVersion();
    return radioVersion == null || radioVersion.isEmpty() || radioVersion.equals("1.0.0.0");
}