web服务监测网络变化,及时更新IP

pull/1854/head
kunfei 3 years ago
parent 254a216e8a
commit fc2bbc4a96
  1. 12
      app/src/main/java/io/legado/app/receiver/NetworkChangedReceiver.kt
  2. 4
      app/src/main/java/io/legado/app/receiver/TimeBatteryReceiver.kt
  3. 3
      app/src/main/java/io/legado/app/service/WebService.kt

@ -4,20 +4,22 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
/**
* 监测网络变化
*/
class NetworkChangedReceiver : BroadcastReceiver() {
var onReceiver: ((context: Context, intent: Intent) -> Unit)? = null
val filter = IntentFilter().apply {
addAction("android.net.conn.CONNECTIVITY_CHANGE")
addAction("android.net.wifi.WIFI_STATE_CHANGED")
addAction("android.net.wifi.STATE_CHANGE")
@Suppress("DEPRECATION")
addAction(ConnectivityManager.CONNECTIVITY_ACTION)
}
override fun onReceive(context: Context?, intent: Intent?) {
override fun onReceive(context: Context, intent: Intent) {
onReceiver?.invoke(context, intent)
}
}

@ -16,8 +16,8 @@ class TimeBatteryReceiver : BroadcastReceiver() {
addAction(Intent.ACTION_BATTERY_CHANGED)
}
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.action) {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
Intent.ACTION_TIME_TICK -> {
postEvent(EventBus.TIME_CHANGED, "")
}

@ -45,6 +45,9 @@ class WebService : BaseService() {
upNotification()
upTile(true)
registerReceiver(networkChangedReceiver, networkChangedReceiver.filter)
networkChangedReceiver.onReceiver = { _, _ ->
upWebServer()
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

Loading…
Cancel
Save