diff --git a/src/main/scala/com/github/shadowsocks/BaseService.scala b/src/main/scala/com/github/shadowsocks/BaseService.scala index e955d2e1..19da9814 100644 --- a/src/main/scala/com/github/shadowsocks/BaseService.scala +++ b/src/main/scala/com/github/shadowsocks/BaseService.scala @@ -49,8 +49,8 @@ import android.content.Context trait BaseService { - var state = State.INIT - var callbackCount = 0 + @volatile var state = State.INIT + @volatile var callbackCount = 0 final val callbacks = new RemoteCallbackList[IShadowsocksServiceCallback] @@ -105,15 +105,17 @@ trait BaseService { handler.post(new Runnable { override def run() { if (state != s) { - val n = callbacks.beginBroadcast() - for (i <- 0 to n - 1) { - try { - callbacks.getBroadcastItem(i).stateChanged(s, msg) - } catch { - case _: Exception => // Ignore + if (callbackCount > 0) { + val n = callbacks.beginBroadcast() + for (i <- 0 to n - 1) { + try { + callbacks.getBroadcastItem(i).stateChanged(s, msg) + } catch { + case _: Exception => // Ignore + } } + callbacks.finishBroadcast() } - callbacks.finishBroadcast() state = s } } diff --git a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala index cbaa4686..b0db7bf5 100644 --- a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala +++ b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala @@ -507,7 +507,7 @@ class Shadowsocks val intent = new Intent(this, s) intent.setAction(Action.SERVICE) bindService(intent, connection, Context.BIND_AUTO_CREATE) - if (!isVpnEnabled) startService(new Intent(this, s)) + startService(new Intent(this, s)) } } diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala index a799f4a8..4357c001 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala @@ -309,6 +309,11 @@ class ShadowsocksNatService extends Service with BaseService { } } + override def onStartCommand(intent: Intent, flags: Int, startId: Int): Int = { + Service.START_NOT_STICKY + } + + def killProcesses() { Console.runRootCommand(Utils.getIptables + " -t nat -F OUTPUT") @@ -328,10 +333,6 @@ class ShadowsocksNatService extends Service with BaseService { Console.runCommand(ab.toArray) } - override def onStartCommand(intent: Intent, flags: Int, startId: Int): Int = { - Service.START_STICKY - } - def flushDNS() { Console.runRootCommand(Array("ndc resolver flushdefaultif", "ndc resolver flushif wlan0")) } @@ -453,9 +454,7 @@ class ShadowsocksNatService extends Service with BaseService { filter.addAction(Action.CLOSE) receiver = new BroadcastReceiver() { def onReceive(p1: Context, p2: Intent) { - spawn { - stopRunner() - } + stopRunner() } } registerReceiver(receiver, filter) @@ -580,7 +579,6 @@ class ShadowsocksNatService extends Service with BaseService { override def stopBackgroundService() { stopRunner() - stopSelf() } override def getTag = TAG diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala index 464f771a..6e19bfdd 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala @@ -253,7 +253,10 @@ class ShadowsocksVpnService extends VpnService with BaseService { notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) .asInstanceOf[NotificationManager] + } + override def onStartCommand(intent: Intent, flags: Int, startId: Int): Int = { + Service.START_NOT_STICKY } override def onRevoke() { @@ -295,9 +298,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { filter.addAction(Intent.ACTION_SHUTDOWN) receiver = new BroadcastReceiver { def onReceive(p1: Context, p2: Intent) { - spawn { - stopRunner() - } + stopRunner() } } registerReceiver(receiver, filter)