Android 动态配置域名
Posted 左郁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 动态配置域名相关的知识,希望对你有一定的参考价值。
应用场景
开发测试过程中,一般会涉及到多个环境测试,例如:开发环境,alpha环境,beta环境,正式环境,测试人员在测试过程中,如果想要查看其他环境的数据,就需要重新安装apk包,所以动态配置域名会显得很方便,不用重新打包,应用内一键切换测试环境
效果展示
- 入口:做了一个彩蛋功能,标题栏连续点击5次进入,入口可添加在任意你想加的地方
- 身份验证:这里加了一个判断,方便开发
- release包需要输入暗号,暗号是当前的日期,格式为yyyyMMdd,eg-20210618,暗号正确,则进入域名配置页面;
- debug包直接进入域名配置页面
- 域名配置:支持的相关操作
- 新增
- 选择
- 删除
实现方式:框架集成
已经集成ZyFrame框架的小伙伴只需要调用两句话
- MyApplication中配置
val defaultHost = "http://10.0.0.110:8088" //默认域名
ZyConfig.HOST = SpUtil.get(HostKey.host_fileName).getString(HostKey.host_config, defaultHost)
- 入口点击事件调用
HostSetUtil.displayEggs(this)
实现方式:源码
- HostSetUtil 域名配置入口彩蛋工具类
/**
* Desc 域名配置入口设置彩蛋
* Author ZY
* Mail sunnyfor98@gmail.com
* Date 2021/6/18 10:37
*/
object HostSetUtil
/**
* 存放点击事件次数
*/
private var mHits: LongArray? = null
/**
* 测试彩蛋
*/
fun displayEggs(context: Context)
if (mHits == null)
mHits = LongArray(3)
mHits?.let
System.arraycopy(mHits ?: longArrayOf(), 1, mHits, 0, it.size - 1)
it[it.size - 1] = SystemClock.uptimeMillis()
if (SystemClock.uptimeMillis() - it[0] <= 5000) //5秒内连续点击。
mHits = null
val isApkInDebug = context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0
if (isApkInDebug)
HostSetDialog(context).show()
else
showAdminDialog(context)
/**
* 展示管理员权限对话框
*/
private fun showAdminDialog(context: Context)
val view = View.inflate(context, R.layout.dialog_host_pwd, null)
val dialog = AlertDialog.Builder(context)
.setTitle("身份验证")
.setView(view)
.create()
dialog.setCanceledOnTouchOutside(false)
dialog.setCancelable(true)
dialog.show()
val cancelBtn = view.findViewById<Button>(R.id.btn_cancel)
val confirmBtn = view.findViewById<Button>(R.id.btn_confirm)
val inputEditText = view.findViewById<EditText>(R.id.et_input)
cancelBtn.setOnClickListener
dialog.dismiss()
confirmBtn.setOnClickListener
val password = StringUtil.getCurrentTime("yyyyMMdd")
if (password == inputEditText.text.toString())
HostSetDialog(context).show()
else
ToastUtil.show("身份验证失败")
dialog.dismiss()
- HostSetDialog 域名设置对话框
/**
* Desc 域名设置对话框
* Author ZY
* Mail sunnyfor98@gmail.com
* Date 2021/6/18 10:37
*/
class HostSetDialog(context: Context) : BaseDialog(context, R.layout.dialog_host_set)
private val mView: View by lazy
LayoutInflater.from(context).inflate(R.layout.dialog_host_set, null, false)
private var hostList = ArrayList<String>()
private val hostAdapter = HostAdapter(hostList).apply
setOnItemClickListener _, position ->
mView.et_input_host.setText(hostList[position])
onItemDeleteListener = position ->
delHostItem(position)
init
setTitle("接口地址配置")
setView(mView)
window?.setLayout(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT
)
window?.setGravity(Gravity.BOTTOM)
setCanceledOnTouchOutside(false)
setCancelable(false)
mView.recyclerView.layoutManager = LinearLayoutManager(context)
mView.recyclerView.adapter = hostAdapter
mView.et_input_host.clearFocus()
SpUtil.get(HostKey.host_fileName).getString(HostKey.host_config).let
mView.et_input_host.setText(if (it.isEmpty()) ZyConfig.HOST else it)
mView.btn_save.setOnClickListener
val host = mView.et_input_host.text.toString().trim()
if (host.isEmpty())
ToastUtil.show("请输入接口地址")
else
dismiss()
if (!hostList.contains(host))
addHostList(host)
ZyConfig.HOST = host
SpUtil.get(HostKey.host_fileName).setString(HostKey.host_config, host)
override fun show()
super.show()
loadHostList()
/**
* 加载 host列表
*/
private fun loadHostList()
hostList.clear()
val json = SpUtil.get(HostKey.host_fileName).getString(HostKey.host_list)
if (json != "")
hostList.addAll(Gson().fromJson(json, object : TypeToken<List<String>>() .type))
else
hostList.add("http://10.0.0.130:8088")
SpUtil.get(HostKey.host_fileName).setString(HostKey.host_list, Gson().toJson(hostList))
hostAdapter.notifyDataSetChanged()
/**
* 添加 host
*/
private fun addHostList(host: String)
hostList.add(0, host)
hostAdapter.notifyDataSetChanged()
SpUtil.get(HostKey.host_fileName).setString(HostKey.host_list, Gson().toJson(hostList))
/**
* 删除 host :先将sp中的字符串转为list,删除相对应的子条目,在存入sp中
*/
private fun delHostItem(position: Int)
hostList.removeAt(position)
hostAdapter.notifyDataSetChanged()
val spList = Gson().fromJson<ArrayList<String>>(
SpUtil.get(HostKey.host_fileName).getString(HostKey.host_list, ""),
object : TypeToken<List<String>>() .type
)
spList.removeAt(position)
SpUtil.get(HostKey.host_fileName).setString(HostKey.host_list, Gson().toJson(spList))
以上是关于Android 动态配置域名的主要内容,如果未能解决你的问题,请参考以下文章