Commit 574f80ae authored by Max Lv's avatar Max Lv

Fix a crash caused by new background limit on Android O

parent 424e1bd1
...@@ -24,6 +24,8 @@ import android.app.Service ...@@ -24,6 +24,8 @@ import android.app.Service
import android.content.Intent import android.content.Intent
import android.net.VpnService import android.net.VpnService
import android.os.{Handler, IBinder} import android.os.{Handler, IBinder}
import android.support.v4.app.NotificationCompat
import android.support.v4.os.BuildCompat
import com.github.shadowsocks.ShadowsocksApplication.app import com.github.shadowsocks.ShadowsocksApplication.app
class ShadowsocksRunnerService extends Service with ServiceBoundContext { class ShadowsocksRunnerService extends Service with ServiceBoundContext {
...@@ -47,11 +49,17 @@ class ShadowsocksRunnerService extends Service with ServiceBoundContext { ...@@ -47,11 +49,17 @@ class ShadowsocksRunnerService extends Service with ServiceBoundContext {
override def onCreate() { override def onCreate() {
super.onCreate() super.onCreate()
if (BuildCompat.isAtLeastO) {
val builder = new NotificationCompat.Builder(this)
builder.setPriority(NotificationCompat.PRIORITY_MIN)
startForeground(1, builder.build)
}
attachService() attachService()
} }
override def onDestroy() { override def onDestroy() {
super.onDestroy() super.onDestroy()
stopForeground(true)
detachService() detachService()
} }
} }
...@@ -28,6 +28,7 @@ import android.content.pm.PackageManager ...@@ -28,6 +28,7 @@ import android.content.pm.PackageManager
import android.content.{Context, Intent} import android.content.{Context, Intent}
import android.graphics._ import android.graphics._
import android.os.Build import android.os.Build
import android.support.v4.os.BuildCompat
import android.util.{Base64, DisplayMetrics, Log} import android.util.{Base64, DisplayMetrics, Log}
import android.view.View.MeasureSpec import android.view.View.MeasureSpec
import android.view.{Gravity, View, Window} import android.view.{Gravity, View, Window}
...@@ -138,7 +139,8 @@ object Utils { ...@@ -138,7 +139,8 @@ object Utils {
def startSsService(context: Context) { def startSsService(context: Context) {
val intent = new Intent(context, classOf[ShadowsocksRunnerService]) val intent = new Intent(context, classOf[ShadowsocksRunnerService])
context.startService(intent) if (BuildCompat.isAtLeastO) context.startForegroundService(intent)
else context.startService(intent)
} }
def stopSsService(context: Context) { def stopSsService(context: Context) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment