mirror of
https://github.com/pppscn/SmsForwarder
synced 2024-11-09 19:10:51 +00:00
新增:网络状态监控通知增加IPv6获取 #321
This commit is contained in:
parent
2bee1dad87
commit
83bca041c9
@ -15,6 +15,7 @@ import androidx.work.workDataOf
|
|||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.idormy.sms.forwarder.R
|
import com.idormy.sms.forwarder.R
|
||||||
import com.idormy.sms.forwarder.entity.MsgInfo
|
import com.idormy.sms.forwarder.entity.MsgInfo
|
||||||
|
import com.idormy.sms.forwarder.utils.CommonUtils
|
||||||
import com.idormy.sms.forwarder.utils.SettingUtils
|
import com.idormy.sms.forwarder.utils.SettingUtils
|
||||||
import com.idormy.sms.forwarder.utils.Worker
|
import com.idormy.sms.forwarder.utils.Worker
|
||||||
import com.idormy.sms.forwarder.workers.SendWorker
|
import com.idormy.sms.forwarder.workers.SendWorker
|
||||||
@ -91,13 +92,18 @@ class NetworkStateService : Service() {
|
|||||||
msg.append(getString(R.string.operator_name)).append(": ").append(operatorName).append("\n")
|
msg.append(getString(R.string.operator_name)).append(": ").append(operatorName).append("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
val inetAddress = NetworkUtils.getLocalInetAddress()
|
//获取IP地址
|
||||||
var hostAddress: String = inetAddress?.hostAddress?.toString() ?: "127.0.0.1"
|
val ipList = CommonUtils.getIPAddresses()
|
||||||
msg.append(getString(R.string.host_address)).append(": ").append(hostAddress).append("\n")
|
|
||||||
|
|
||||||
if (ServiceUtils.isServiceRunning("com.idormy.sms.forwarder.service.HttpService")) {
|
if (ServiceUtils.isServiceRunning("com.idormy.sms.forwarder.service.HttpService")) {
|
||||||
hostAddress = if (hostAddress.indexOf(':', 0, false) > 0) "[${hostAddress}]" else hostAddress
|
ipList.forEach() {
|
||||||
msg.append(getString(R.string.http_server)).append(": ").append("http://${hostAddress}:5000")
|
msg.append(getString(R.string.host_address)).append(": ").append(it).append("\n")
|
||||||
|
val hostAddress = if (it.indexOf(':', 0, false) > 0) "[${CommonUtils.removeInterfaceFromIP(it)}]" else it
|
||||||
|
msg.append(getString(R.string.http_server)).append(": ").append("http://${hostAddress}:5000").append("\n")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ipList.forEach() {
|
||||||
|
msg.append(getString(R.string.host_address)).append(": ").append(it).append("\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessage(context, msg.toString())
|
sendMessage(context, msg.toString())
|
||||||
|
@ -35,6 +35,9 @@ import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog.SingleButton
|
|||||||
import com.xuexiang.xui.widget.imageview.preview.PreviewBuilder
|
import com.xuexiang.xui.widget.imageview.preview.PreviewBuilder
|
||||||
import com.xuexiang.xutil.XUtil
|
import com.xuexiang.xutil.XUtil
|
||||||
import com.xuexiang.xutil.common.StringUtils
|
import com.xuexiang.xutil.common.StringUtils
|
||||||
|
import java.net.Inet4Address
|
||||||
|
import java.net.Inet6Address
|
||||||
|
import java.net.NetworkInterface
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -277,6 +280,40 @@ class CommonUtils private constructor() {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//获取本机IP地址
|
||||||
|
fun getIPAddresses(): List<String> {
|
||||||
|
val ipAddresses = mutableListOf<String>()
|
||||||
|
|
||||||
|
try {
|
||||||
|
val networkInterfaces = NetworkInterface.getNetworkInterfaces()
|
||||||
|
|
||||||
|
while (networkInterfaces.hasMoreElements()) {
|
||||||
|
val networkInterface = networkInterfaces.nextElement()
|
||||||
|
val addresses = networkInterface.inetAddresses
|
||||||
|
|
||||||
|
while (addresses.hasMoreElements()) {
|
||||||
|
val address = addresses.nextElement()
|
||||||
|
|
||||||
|
if (address is Inet4Address || address is Inet6Address) {
|
||||||
|
ipAddresses.add(address.hostAddress)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
|
||||||
|
return ipAddresses
|
||||||
|
}
|
||||||
|
|
||||||
|
fun removeInterfaceFromIP(ipAddress: String): String {
|
||||||
|
val index = ipAddress.indexOf("%")
|
||||||
|
return if (index != -1) {
|
||||||
|
ipAddress.substring(0, index)
|
||||||
|
} else {
|
||||||
|
ipAddress
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
Loading…
Reference in New Issue
Block a user