用kotlin 语言Android 双卡切换判断移动数据是否开启
private var telephonyManager: TelephonyManager? = null private var dialogBuilder: MaterialAlertDialogBuilder? = null private var dialog: AlertDialog? = null
override fun onResume() {
super.onResume()
telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager?
telephonyManager!!.listen(myPhoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
dialogBuilder = MaterialAlertDialogBuilder(this)
.setTitle("温馨提示")
.setMessage("请打开移动数据,否则可能无法正常使用")
.setPositiveButton("知道了") { _, _ ->
closeApp()
}
.setNegativeButton("去打开") { _, _ ->
openMobileData()
}
.setCancelable(false)
}
override fun onPause() {
super.onPause()
Timber.i("onPause Activity ${javaClass.simpleName}")
telephonyManager!!.listen(myPhoneStateListener, PhoneStateListener.LISTEN_NONE);
}
val myPhoneStateListener = object : PhoneStateListener() {
override fun onDataConnectionStateChanged(state: Int,networkType: Int) {
super.onDataConnectionStateChanged(state)
//监听流量状态
BLogUtils.e("VectorBaseActivity", "===========onDataConnectionStateChanged====state==========" + state+"networkType"+networkType)
if (state == TelephonyManager.DATA_DISCONNECTED&&networkType == 20) {
//移动数据关闭
dialog = dialogBuilder!!.show()
} else if(state == TelephonyManager.DATA_DISCONNECTED&&networkType == 13) {
//移动数据开启 副卡
if (dialog != null) {
dialog!!.dismiss()
}
} else if(state == TelephonyManager.DATA_CONNECTED){
//移动数据开启 主卡
if (dialog != null) {
dialog!!.dismiss()
}
}
}
}
本文介绍了一种使用Kotlin语言在Android应用中监测移动数据状态的方法,包括判断双卡切换时移动数据是否开启,并通过对话框提示用户进行相应操作。
2307

被折叠的 条评论
为什么被折叠?



