在andriod项目开发过程中,我们时常有这样的需求,例如:动态切换域名,动态切换abtest,动态切换服务器配置项等,但是开发过程中总是让服务器配合修改或者每次都修改代码后运行就很麻烦,所以我们就有了一个开发需求,即app运行过程中进行动态配置。
那么我们怎么做好一些呢?
这里推荐单独开发一个develop页面调试工具,并配置单独的入口,如图所示:
Screenshot_20230829_211904_One UI Home.jpg
其中androidDemo为主项目入口,developer tools为调试页面入口。
接下来我们来简单配置一下如何能够达到文中的效果。
首先我们在src路径下创建debug文件夹,并创建DeveloperToolsAct,manifest.xml文件等,这样做的好处是只有debug模式才显示该入口。
Screenshot_20230829_211904_One UI Home.jpg然后我们修改debug/AndroidManifest.xml文件,将DeveloperToolsAct配置成项目入口。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application tools:ignore="MissingApplicationIcon">
<activity
android:name="yz.l.fm.DeveloperToolsAct"
android:clearTaskOnLaunch="true"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:excludeFromRecents="true"
android:exported="true"
android:finishOnTaskLaunch="true"
android:label="developer tools"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:taskAffinity=".developer"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
此时,我们debug模式运行项目,发现程序已经有两个入口了,但是默认启动项可能是DeveloperToolsAct。
如果默认启动的Activity不是预期的,那么我们仅需要在想要默认启动的activity配置中添加Default即可,代码如下:
<activity
android:name="yz.l.fm.SplashAct"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:exported="true"
android:launchMode="singleTask"
android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" /> <!--添加这一行即可-->
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
此时我们仅需要在DeveloperToolsAct中写相关的动态切换代码即可达到目的。
网友评论