如何以编程方式检查应用程序是否在调试模式下运行?

时间:2011-08-11 08:03:09

标签: java android

我必须在我的应用中的某个地方识别我的应用是以调试模式还是实时模式运行。是否有任何功能或代码可用于检查。在任何一种情况下都会返回true / false开/关。如果是的话,请帮帮我。提前谢谢。

4 个答案:

答案 0 :(得分:34)

用户@ Im0rtality在评论中提供的answer是正确的:

boolean isDebuggable = (0 != (getApplicationInfo().flags
                              & ApplicationInfo.FLAG_DEBUGGABLE));

(来自this Google blog post

相关:

答案 1 :(得分:27)

从调查模式是否涉及的问题不清楚:

  1. 该应用程序是否可调试
  2. 当前是否正在调试应用(例如,通过ADB)
  3. 第一个由CommonsWare's answer覆盖:

    boolean isDebuggable = 0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE);
    

    第二个是:

    boolean isBeingDebugged = android.os.Debug.isDebuggerConnected()
    

    https://developer.android.com/reference/android/os/Debug.html#isDebuggerConnected()

答案 2 :(得分:13)

if (BuildConfig.DEBUG) {
  // here be thine debug statement
}

在eclipse和Android Studio上工作得很好。

这里提到的其他内容经常会为我抛出运行时异常

答案 3 :(得分:12)

如果通过“实时模式”表示已在Play商店中使用,则可以通过检查BuildConfig.DEBUG的值来区分这两种状态。 Google已经展示了一段关于它的视频here

相关问题