From 4308e90f2605509fb171a04f4ace3a1fef7ea781 Mon Sep 17 00:00:00 2001 From: Graham Jones Date: Mon, 14 Jan 2019 23:31:02 +0000 Subject: [PATCH] Solved problem of background service crashing - it is a 'feature' of Android 8 where background services are shutdown when the main activity stops. Solution is to make the service a 'Foreground Service' instead (I think - testing now....) --- .../uk/org/openseizuredetector/OsdUtil.java | 9 ++++++++- .../uk/org/openseizuredetector/SdServer.java | 19 +++++++++++++------ build.gradle | 3 ++- gradle/wrapper/gradle-wrapper.properties | 4 ++-- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java b/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java index 1132bcc..7861872 100644 --- a/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java +++ b/app/src/main/java/uk/org/openseizuredetector/OsdUtil.java @@ -49,6 +49,7 @@ import android.content.res.XmlResourceParser; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.os.Build; import android.os.Environment; import android.os.Handler; import android.os.IBinder; @@ -164,7 +165,13 @@ public class OsdUtil { Intent sdServerIntent; sdServerIntent = new Intent(mContext, SdServer.class); sdServerIntent.setData(Uri.parse("Start")); - mContext.startService(sdServerIntent); + if (Build.VERSION.SDK_INT >= 26) { + Log.i(TAG,"Starting Foreground Service (Android 8 and above)"); + mContext.startForegroundService(sdServerIntent); + } else { + Log.i(TAG,"Starting Normal Service (Pre-Android 8)"); + mContext.startService(sdServerIntent); + } } /** diff --git a/app/src/main/java/uk/org/openseizuredetector/SdServer.java b/app/src/main/java/uk/org/openseizuredetector/SdServer.java index c208925..0c9b5ff 100644 --- a/app/src/main/java/uk/org/openseizuredetector/SdServer.java +++ b/app/src/main/java/uk/org/openseizuredetector/SdServer.java @@ -84,6 +84,7 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei private NotificationManager mNM; private NotificationCompat.Builder mNotificationBuilder; + private Notification mNotification; private SdWebServer webServer = null; private final static String TAG = "SdServer"; private Timer dataLogTimer = null; @@ -243,10 +244,16 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei // Display a notification icon in the status bar of the phone to // show the service is running. - Log.v(TAG, "showing Notification"); - mUtil.writeToSysLogFile("SdServer.onStartCommand() - showing Notification"); - showNotification(0); - + if (Build.VERSION.SDK_INT >= 26) { + Log.v(TAG, "showing Notification and calling startForeground (Android 8 and higher)"); + mUtil.writeToSysLogFile("SdServer.onStartCommand() - showing Notification and calling startForeground (Android 8 and higher)"); + showNotification(0); + startForeground(NOTIFICATION_ID,mNotification); + } else { + Log.v(TAG, "showing Notification"); + mUtil.writeToSysLogFile("SdServer.onStartCommand() - showing Notification"); + showNotification(0); + } // Record last time we sent an SMS so we can limit rate of SMS // sending to one per minute. mSMSTime = new Time(Time.getCurrentTimezone()); @@ -375,7 +382,7 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); - Notification notification = mNotificationBuilder.setContentIntent(contentIntent) + mNotification = mNotificationBuilder.setContentIntent(contentIntent) .setSmallIcon(iconId) .setColor(0xff000000) .setTicker("OpenSeizureDetector") @@ -385,7 +392,7 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei .setOnlyAlertOnce(true) .build(); - mNM.notify(NOTIFICATION_ID, notification); + mNM.notify(NOTIFICATION_ID, mNotification); } // Show the main activity on the user's screen. diff --git a/build.gradle b/build.gradle index 2f60b76..1dcd6aa 100644 --- a/build.gradle +++ b/build.gradle @@ -6,9 +6,10 @@ buildscript { url 'https://maven.google.com/' name 'Google' } + google() } dependencies { - classpath 'com.android.tools.build:gradle:3.2.1' + classpath 'com.android.tools.build:gradle:3.3.0' } } allprojects { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index d791226..a9e9b6e 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Dec 26 18:31:40 GMT 2018 +#Mon Jan 14 22:11:00 GMT 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip