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.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.net.ConnectivityManager
/** /**
* 监测网络变化 * 监测网络变化
*/ */
class NetworkChangedReceiver : BroadcastReceiver() { class NetworkChangedReceiver : BroadcastReceiver() {
var onReceiver: ((context: Context, intent: Intent) -> Unit)? = null
val filter = IntentFilter().apply { val filter = IntentFilter().apply {
addAction("android.net.conn.CONNECTIVITY_CHANGE") @Suppress("DEPRECATION")
addAction("android.net.wifi.WIFI_STATE_CHANGED") addAction(ConnectivityManager.CONNECTIVITY_ACTION)
addAction("android.net.wifi.STATE_CHANGE")
} }
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) addAction(Intent.ACTION_BATTERY_CHANGED)
} }
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context, intent: Intent) {
when (intent?.action) { when (intent.action) {
Intent.ACTION_TIME_TICK -> { Intent.ACTION_TIME_TICK -> {
postEvent(EventBus.TIME_CHANGED, "") postEvent(EventBus.TIME_CHANGED, "")
} }

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

Loading…
Cancel
Save