| | |
| | | |
| | | import android.content.Intent |
| | | import android.os.Bundle |
| | | import android.os.Handler |
| | | import android.os.Looper |
| | | import android.view.KeyEvent |
| | | import android.view.LayoutInflater |
| | | import android.widget.Toast |
| | |
| | | import com.dayu.general.R |
| | | import com.dayu.general.adapter.TabAdapter |
| | | import com.dayu.general.bean.net.UserInfoResult |
| | | import com.dayu.general.bean.net.WaterPriceResult |
| | | import com.dayu.general.databinding.ActivityMainBinding |
| | | import com.dayu.general.net.ApiManager |
| | | import com.dayu.general.net.BaseResponse |
| | |
| | | var binding: ActivityMainBinding? = null |
| | | private val fragments: ArrayList<Fragment> = ArrayList() |
| | | var mExitTime: Long = 0 |
| | | private val handler = Handler(Looper.getMainLooper()) |
| | | |
| | | override fun onCreate(savedInstanceState: Bundle?) { |
| | | super.onCreate(savedInstanceState) |
| | | binding = ActivityMainBinding.inflate(LayoutInflater.from(this)) |
| | | setContentView(binding?.root) |
| | | |
| | | // 注册MainActivity实例到BaseApplication |
| | | BaseApplication.setMainActivity(this) |
| | | |
| | | setupFragments() |
| | | initView() |
| | | initTab() |
| | | getUserInfo() |
| | | |
| | | // 延时20秒后获取水价 |
| | | handler.postDelayed({ |
| | | getWaterPriceFromActivity() |
| | | }, 20000) // 20秒延时 |
| | | } |
| | | |
| | | override fun onDestroy() { |
| | | super.onDestroy() |
| | | // 清理Handler回调,防止内存泄漏 |
| | | handler.removeCallbacksAndMessages(null) |
| | | // 清理BaseApplication中的MainActivity引用 |
| | | BaseApplication.setMainActivity(null) |
| | | } |
| | | |
| | | override fun onNfcBack(intent: Intent) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取水价信息 - 公开方法供其他地方调用 |
| | | */ |
| | | fun getWaterPriceFromActivity() { |
| | | // 如果水价已存在且大于0,则不重复获取 |
| | | if (BaseApplication.waterPrice > 0.0) { |
| | | return |
| | | } |
| | | |
| | | ApiManager.getInstance().requestGetHideLoading( |
| | | this, |
| | | "terminal/client/getWaterPrice", |
| | | WaterPriceResult::class.java, |
| | | null, |
| | | object : SubscriberListener<BaseResponse<WaterPriceResult>>() { |
| | | override fun onNext(response: BaseResponse<WaterPriceResult>) { |
| | | if (response.success && response.code == "0001") { |
| | | // 获取水价成功,保存到BaseApplication |
| | | response.content?.let { waterPriceResult -> |
| | | BaseApplication.waterPrice = waterPriceResult.price |
| | | } |
| | | } |
| | | } |
| | | |
| | | override fun onError(e: Throwable?) { |
| | | super.onError(e) |
| | | // 网络异常时不显示错误信息,避免影响用户体验 |
| | | } |
| | | } |
| | | ) |
| | | } |
| | | |
| | | private fun getUserInfo() { |
| | | // 使用正确的类型参数 |