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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user