Only show menu item in debug mode?
In my app I have created a test page with some code that only will be available for the developer. This page/activity can only be reached via a menu-item. Is t开发者_运维技巧here a way to determine if an android app is running in debug mode, and only show the menu-item when this is true?
regards, Goldhorn
To directly check debug mode in menu or another resource XML, we can define resValue in build.gradle
like below.
buildTypes {
release {
...
resValue "bool", "DEBUG", "false"
}
debug {
...
resValue "bool", "DEBUG", "true"
}
}
In generated.xml
we will get:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Automatically generated file. DO NOT MODIFY -->
<!-- Values from build type: debug -->
<bool name="DEBUG">true</bool>
</resources>
Now we can use this values and in menu.xml
:
<item android:visible="@bool/DEBUG" ... />
You need to check if the debuggable="true" is set in AndroidManifest.xml. See this post for more info - Getting "debuggable" value of androidManifest from code?
USE THIS HACK
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String nOp = tm.getNetworkOperatorName();
if("Android".equals(nOp)) {
// ADD YOUR MENUS FOR EMULATOR
}
else {
// ADD YOUR MENUS FOR DEVICE
}
精彩评论