Fix a crash caused by new background limit on Android O

This commit is contained in:
Max Lv
2017-05-26 18:01:07 +08:00
parent 424e1bd1bc
commit 574f80aefd
2 changed files with 11 additions and 1 deletions
@@ -24,6 +24,8 @@ import android.app.Service
import android.content.Intent
import android.net.VpnService
import android.os.{Handler, IBinder}
import android.support.v4.app.NotificationCompat
import android.support.v4.os.BuildCompat
import com.github.shadowsocks.ShadowsocksApplication.app
class ShadowsocksRunnerService extends Service with ServiceBoundContext {
@@ -47,11 +49,17 @@ class ShadowsocksRunnerService extends Service with ServiceBoundContext {
override def onCreate() {
super.onCreate()
if (BuildCompat.isAtLeastO) {
val builder = new NotificationCompat.Builder(this)
builder.setPriority(NotificationCompat.PRIORITY_MIN)
startForeground(1, builder.build)
}
attachService()
}
override def onDestroy() {
super.onDestroy()
stopForeground(true)
detachService()
}
}
@@ -28,6 +28,7 @@ import android.content.pm.PackageManager
import android.content.{Context, Intent}
import android.graphics._
import android.os.Build
import android.support.v4.os.BuildCompat
import android.util.{Base64, DisplayMetrics, Log}
import android.view.View.MeasureSpec
import android.view.{Gravity, View, Window}
@@ -138,7 +139,8 @@ object Utils {
def startSsService(context: Context) {
val intent = new Intent(context, classOf[ShadowsocksRunnerService])
context.startService(intent)
if (BuildCompat.isAtLeastO) context.startForegroundService(intent)
else context.startService(intent)
}
def stopSsService(context: Context) {