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....)

This commit is contained in:
Graham Jones
2019-01-14 23:31:02 +00:00
parent 77c3f9d48e
commit 4308e90f26
4 changed files with 25 additions and 10 deletions

View File

@@ -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);
}
}
/**

View File

@@ -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.

View File

@@ -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 {

View File

@@ -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