desc:监听sim卡是否可用及信号强度
parent
a4dadd3d6f
commit
795810be52
@ -0,0 +1,109 @@
|
|||||||
|
package com.yinuo.safetywatcher.watcher.utils
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.app.Service
|
||||||
|
import android.content.BroadcastReceiver
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.IntentFilter
|
||||||
|
import android.telephony.PhoneStateListener
|
||||||
|
import android.telephony.SignalStrength
|
||||||
|
import android.telephony.SubscriptionInfo
|
||||||
|
import android.telephony.SubscriptionManager
|
||||||
|
import android.telephony.TelephonyManager
|
||||||
|
import android.util.Log
|
||||||
|
|
||||||
|
|
||||||
|
object SimHelper {
|
||||||
|
private var callbacks = mutableListOf<OnSimLevelCallback>()
|
||||||
|
private var mSimState = -1;
|
||||||
|
private var mRssi = -1;
|
||||||
|
|
||||||
|
private val receiver = object : BroadcastReceiver() {
|
||||||
|
override fun onReceive(context: Context?, intent: Intent?) {
|
||||||
|
if (intent?.action.equals("android.intent.action.SIM_STATE_CHANGED")) {
|
||||||
|
val tm = context!!.getSystemService(Service.TELEPHONY_SERVICE) as TelephonyManager
|
||||||
|
mSimState = tm.simState
|
||||||
|
}
|
||||||
|
Log.i(
|
||||||
|
this@SimHelper.javaClass.name,
|
||||||
|
"onReceive action = ${intent?.action}, mSimState = $mSimState, mRssi = $mRssi"
|
||||||
|
)
|
||||||
|
callbacks.forEach {
|
||||||
|
it.onLevel(
|
||||||
|
mSimState == TelephonyManager.SIM_STATE_READY, mRssi
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun init(context: Context) {
|
||||||
|
watchSimState(context)
|
||||||
|
watchSimRssi(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun watchSimRssi(context: Context) {
|
||||||
|
val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
|
||||||
|
//获取指定卡槽的信号强度
|
||||||
|
val info = getNewerSlotSubscriptionInfo(context, 0)
|
||||||
|
info?.let {
|
||||||
|
val mTelephonyManagerForSubscriptionId =
|
||||||
|
tm.createForSubscriptionId(info!!.subscriptionId)
|
||||||
|
mTelephonyManagerForSubscriptionId.listen(object : PhoneStateListener() {
|
||||||
|
override fun onSignalStrengthsChanged(signalStrength: SignalStrength) {
|
||||||
|
super.onSignalStrengthsChanged(signalStrength)
|
||||||
|
mRssi = signalStrength.level
|
||||||
|
callbacks.forEach {
|
||||||
|
it.onLevel(
|
||||||
|
mSimState == TelephonyManager.SIM_STATE_READY, mRssi
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun watchSimState(context: Context) {
|
||||||
|
val filter = IntentFilter("android.intent.action.SIM_STATE_CHANGED")
|
||||||
|
context.registerReceiver(receiver, filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addCallBack(callBack: OnSimLevelCallback) {
|
||||||
|
callbacks.add(callBack)
|
||||||
|
if (mSimState != -1) {
|
||||||
|
callBack.onLevel(mSimState == TelephonyManager.SIM_STATE_READY, mRssi)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeCallback(callBack: OnSimLevelCallback) {
|
||||||
|
callbacks.remove(callBack)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun release(context: Context) {
|
||||||
|
callbacks.clear()
|
||||||
|
context.unregisterReceiver(receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OnSimLevelCallback {
|
||||||
|
fun onLevel(enable: Boolean, level: Int)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressLint("MissingPermission")
|
||||||
|
@Synchronized
|
||||||
|
fun getNewerSlotSubscriptionInfo(context: Context, slot: Int): SubscriptionInfo? {
|
||||||
|
val list = SubscriptionManager.from(context).activeSubscriptionInfoList
|
||||||
|
if (list != null) {
|
||||||
|
for (info in list) {
|
||||||
|
if (slot == info.simSlotIndex) {
|
||||||
|
Log.i(
|
||||||
|
this@SimHelper.javaClass.name,
|
||||||
|
"SlotIndex : " + info.simSlotIndex + " displayName : " + info.displayName
|
||||||
|
)
|
||||||
|
return info
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue