以编程方式在Manifest中读取调试状态

时间:2011-11-11 17:34:41

标签: android xml manifest xdebug

我想读取Android清单文件中的调试状态,然后根据该状态触发方法。我看到你可以读取XML文件并解析它,但这种方式看起来并不那么优雅。还有另外一种方法,就是将Manifest中的什么信息存储在某个Java对象中?

<application android:name=".MyActivity" android:icon="@drawable/myicon"
    android:label="@string/app_name" android:debuggable="true">

2 个答案:

答案 0 :(得分:17)

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

答案 1 :(得分:2)

我使用ApplicationInfo.FLAG_DEBUGGABLE来检查是否设置了android:debuggable = true。 以下代码是从此thread

复制而来的
private static Boolean isSignedWithDebugKey = null;     
    protected boolean signedWithDebug() {         
        if(isSignedWithDebugKey == null) {             
            PackageManager pm = getPackageManager();             
            try {                
             PackageInfo pi = pm.getPackageInfo(getPackageName(), 0);                 
                isSignedWithDebugKey = (pi.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;             
            }             
            catch(NameNotFoundException nnfe) {                 
                nnfe.printStackTrace();                 
                isSignedWithDebugKey = false;             
            }         
        }         
         return isSignedWithDebugKey;     
    }