diff --git a/src/main/scala/com/github/shadowsocks/BaseService.scala b/src/main/scala/com/github/shadowsocks/BaseService.scala index a2b7528b..67ea792d 100644 --- a/src/main/scala/com/github/shadowsocks/BaseService.scala +++ b/src/main/scala/com/github/shadowsocks/BaseService.scala @@ -42,7 +42,7 @@ package com.github.shadowsocks import java.util.{Timer, TimerTask} import android.app.Service -import android.content.Context +import android.content.{Intent, Context} import android.os.{Handler, RemoteCallbackList} import com.github.shadowsocks.aidl.{Config, IShadowsocksService, IShadowsocksServiceCallback} import com.github.shadowsocks.utils.{State, TrafficMonitor, TrafficMonitorThread} @@ -110,6 +110,7 @@ trait BaseService extends Service { def startRunner(config: Config) { this.config = config + startService(new Intent(getContext, getClass)) TrafficMonitor.reset() trafficMonitorThread = new TrafficMonitorThread() trafficMonitorThread.start() @@ -124,6 +125,12 @@ trait BaseService extends Service { trafficMonitorThread.stopThread() trafficMonitorThread = null } + + // change the state + changeState(State.STOPPED) + + // stop the service if nothing has bound to it + stopSelf() } def updateTrafficTotal(tx: Long, rx: Long) { diff --git a/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala b/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala index c304f559..8a68a761 100644 --- a/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala +++ b/src/main/scala/com/github/shadowsocks/ServiceBoundContext.scala @@ -54,8 +54,6 @@ trait ServiceBoundContext extends Context { connection = new ShadowsocksServiceConnection() bindService(intent, connection, Context.BIND_AUTO_CREATE) - - 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 93cf2327..45f93c87 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala @@ -458,8 +458,6 @@ class ShadowsocksNatService extends BaseService { override def stopRunner() { - super.stopRunner() - // channge the state changeState(State.STOPPING) @@ -476,11 +474,7 @@ class ShadowsocksNatService extends BaseService { // reset NAT killProcesses() - // stop the service if nothing has bound to it - stopSelf() - - // change the state - changeState(State.STOPPED) + super.stopRunner() } 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 9e3c5f31..d0ecfeaf 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala @@ -113,8 +113,6 @@ class ShadowsocksVpnService extends VpnService with BaseService { override def stopRunner() { - super.stopRunner() - if (vpnThread != null) { vpnThread.stopThread() vpnThread = null @@ -136,17 +134,13 @@ class ShadowsocksVpnService extends VpnService with BaseService { conn = null } - // stop the service if nothing has bound to it - stopSelf() - // clean up recevier if (closeReceiver != null) { unregisterReceiver(closeReceiver) closeReceiver = null } - // channge the state - changeState(State.STOPPED) + super.stopRunner() } def getVersionName: String = {