Skip to content

Commit

Permalink
Fix a crash caused by new background limit on Android O
Browse files Browse the repository at this point in the history
  • Loading branch information
madeye committed May 26, 2017
1 parent 424e1bd commit 574f80a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 574f80a

Please sign in to comment.