问题描述
我用 Worker 做了一个 android 例程。该例程从设备获取一些信息,如下面的代码所示。我的问题是从广播中获取 wifi 列表并将其发送到例程。我尝试使用协程和内部函数广播,但没有任何工作可以将数据返回到工作程序
我的类DeviceInfo,收集数据的地方:
class DeviceInfo(private val context: Context) {
private var listNetworkAvailable = mutablelistof<String>()
// I'm trying to do with the broadcast a way to save and return the wifi list to be getted from my worker class
suspend fun getCurrentScanResults(): MutableList<String> {
val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as? WifiManager ?: return mutablelistof()
return suspendCancellableCoroutine { continuation ->
val wifiScanReceiver = object : broadcastReceiver() {
override fun onReceive(c: Context,intent: Intent) {
println("listNetworkAvailable start")
if (intent.action == WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION) {
context.unregisterReceiver(this)
for (i in (wifiManager.scanResults as MutableList<ScanResult>?)!!.indices) {
println("listNetworkAvailable")
println(listNetworkAvailable)
val ssid = (wifiManager.scanResults as MutableList<ScanResult>?)!![i].toString()
.split(",")[0].replace("SSID: ","")
if (ssid != "") {
listNetworkAvailable.add(ssid)
}
}
continuation.resume(listNetworkAvailable)
}
}
}
continuation.invokeOnCancellation {
context.unregisterReceiver(wifiScanReceiver)
}
context.registerReceiver(wifiScanReceiver,IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION))
wifiManager.startScan()
println("device info wifiManager")
println(wifiManager)
}
}
val vpnConnected: Boolean
get(){
var result = false
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
cm?.run {
cm.activeNetworkInfo?.run {
if (type == ConnectivityManager.TYPE_VPN) {
result = true
}
}
}
return result
}
val wifiOr3G: String
get(){
try {
var result = "none"
//0 // Returns connection type. 0: none; 1: mobile data; 2: wifi; 3: VPN
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
cm?.run {
cm.getNetworkCapabilities(cm.activeNetwork)?.run {
if (hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
result = "Wifi"
} else if (hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
result = "Mobile data"
}
}
}
return result
} catch (e: Exception){
return "Excecao disparada: $e";
}
}
val networkClass: String
get() {
return try {
val mTelephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
val networkType = mTelephonyManager.networkType
when (networkType) {
TelephonyManager.NETWORK_TYPE_GPRS,TelephonyManager.NETWORK_TYPE_EDGE,TelephonyManager.NETWORK_TYPE_CDMA,TelephonyManager.NETWORK_TYPE_1xRTT,TelephonyManager.NETWORK_TYPE_IDEN -> NETWORK_TYPE_2G
TelephonyManager.NETWORK_TYPE_umts,TelephonyManager.NETWORK_TYPE_EVDO_0,TelephonyManager.NETWORK_TYPE_EVDO_A,TelephonyManager.NETWORK_TYPE_HSDPA,TelephonyManager.NETWORK_TYPE_HSUPA,TelephonyManager.NETWORK_TYPE_HSPA,TelephonyManager.NETWORK_TYPE_EVDO_B,TelephonyManager.NETWORK_TYPE_EHRPD,TelephonyManager.NETWORK_TYPE_HSPAP -> NETWORK_TYPE_3G
TelephonyManager.NETWORK_TYPE_LTE -> NETWORK_TYPE_4G
else -> NOT_FOUND_VAL
}
}catch (e: Exception){
"Excecao disparada: $e";
}
}
}
我需要数据的工人阶级: class Process(appContext: Context,workerParams: WorkerParameters) : Worker(appContext,workerParams) {
override fun doWork(): Result {
//The getCurrentScanResults() element is the one I need to get correctly
val json = """{
"network": {
"available_wifi_list": """+DeviceInfo(applicationContext).getCurrentScanResults()+""","vpn_connected": """"+DeviceInfo(applicationContext).vpnConnected.toString()+"""","type": """"+DeviceInfo(applicationContext).wifiOr3G+"""","class": """"+DeviceInfo(applicationContext).networkClass+""""
}
}"""
sendToServer(json)
return Result.success()
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)