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.Rect;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
@@ -164,7 +165,13 @@ public class OsdUtil {
|
|||||||
Intent sdServerIntent;
|
Intent sdServerIntent;
|
||||||
sdServerIntent = new Intent(mContext, SdServer.class);
|
sdServerIntent = new Intent(mContext, SdServer.class);
|
||||||
sdServerIntent.setData(Uri.parse("Start"));
|
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 NotificationManager mNM;
|
||||||
private NotificationCompat.Builder mNotificationBuilder;
|
private NotificationCompat.Builder mNotificationBuilder;
|
||||||
|
private Notification mNotification;
|
||||||
private SdWebServer webServer = null;
|
private SdWebServer webServer = null;
|
||||||
private final static String TAG = "SdServer";
|
private final static String TAG = "SdServer";
|
||||||
private Timer dataLogTimer = null;
|
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
|
// Display a notification icon in the status bar of the phone to
|
||||||
// show the service is running.
|
// show the service is running.
|
||||||
Log.v(TAG, "showing Notification");
|
if (Build.VERSION.SDK_INT >= 26) {
|
||||||
mUtil.writeToSysLogFile("SdServer.onStartCommand() - showing Notification");
|
Log.v(TAG, "showing Notification and calling startForeground (Android 8 and higher)");
|
||||||
showNotification(0);
|
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
|
// Record last time we sent an SMS so we can limit rate of SMS
|
||||||
// sending to one per minute.
|
// sending to one per minute.
|
||||||
mSMSTime = new Time(Time.getCurrentTimezone());
|
mSMSTime = new Time(Time.getCurrentTimezone());
|
||||||
@@ -375,7 +382,7 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei
|
|||||||
PendingIntent contentIntent =
|
PendingIntent contentIntent =
|
||||||
PendingIntent.getActivity(this,
|
PendingIntent.getActivity(this,
|
||||||
0, i, PendingIntent.FLAG_UPDATE_CURRENT);
|
0, i, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
Notification notification = mNotificationBuilder.setContentIntent(contentIntent)
|
mNotification = mNotificationBuilder.setContentIntent(contentIntent)
|
||||||
.setSmallIcon(iconId)
|
.setSmallIcon(iconId)
|
||||||
.setColor(0xff000000)
|
.setColor(0xff000000)
|
||||||
.setTicker("OpenSeizureDetector")
|
.setTicker("OpenSeizureDetector")
|
||||||
@@ -385,7 +392,7 @@ public class SdServer extends Service implements SdDataReceiver, SdLocationRecei
|
|||||||
.setOnlyAlertOnce(true)
|
.setOnlyAlertOnce(true)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
mNM.notify(NOTIFICATION_ID, notification);
|
mNM.notify(NOTIFICATION_ID, mNotification);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show the main activity on the user's screen.
|
// Show the main activity on the user's screen.
|
||||||
|
|||||||
@@ -6,9 +6,10 @@ buildscript {
|
|||||||
url 'https://maven.google.com/'
|
url 'https://maven.google.com/'
|
||||||
name 'Google'
|
name 'Google'
|
||||||
}
|
}
|
||||||
|
google()
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.android.tools.build:gradle:3.2.1'
|
classpath 'com.android.tools.build:gradle:3.3.0'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
allprojects {
|
allprojects {
|
||||||
|
|||||||
4
gradle/wrapper/gradle-wrapper.properties
vendored
4
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,6 +1,6 @@
|
|||||||
#Wed Dec 26 18:31:40 GMT 2018
|
#Mon Jan 14 22:11:00 GMT 2019
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user