diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java
index 54b7a07d..f72e8e4b 100644
--- a/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java
+++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/SyncthingService.java
@@ -97,6 +97,7 @@ public class SyncthingService extends Service implements
public static final String PREF_USE_ROOT = "use_root";
private static final String PREF_NOTIFICATION_TYPE = "notification_type";
public static final String PREF_USE_WAKE_LOCK = "wakelock_while_binary_running";
+ public static final String PREF_FOREGROUND_SERVICE = "run_as_foreground_service";
private static final int NOTIFICATION_ACTIVE = 1;
@@ -291,6 +292,13 @@ public class SyncthingService extends Service implements
private void updateNotification() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String type = sp.getString(PREF_NOTIFICATION_TYPE, "low_priority");
+ boolean foreground = sp.getBoolean(PREF_FOREGROUND_SERVICE, false);
+ if ("none".equals(type) && foreground) {
+ // foreground priority requires any notification
+ // so this ensures that we either have a "default" or "low_priority" notification,
+ // but not "none".
+ type = "low_priority";
+ }
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if ((mCurrentState == State.ACTIVE || mCurrentState == State.STARTING) &&
!type.equals("none")) {
@@ -304,15 +312,23 @@ public class SyncthingService extends Service implements
if (type.equals("low_priority"))
builder.setPriority(NotificationCompat.PRIORITY_MIN);
- nm.notify(NOTIFICATION_ACTIVE, builder.build());
+ if (foreground) {
+ builder.setContentText(getString(R.string.syncthing_active_foreground));
+ startForeground(NOTIFICATION_ACTIVE, builder.build());
+ } else {
+ stopForeground(false); // ensure no longer running with foreground priority
+ nm.notify(NOTIFICATION_ACTIVE, builder.build());
+ }
} else {
+ // ensure no longer running with foreground priority
+ stopForeground(false);
nm.cancel(NOTIFICATION_ACTIVE);
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
- if (key.equals(PREF_NOTIFICATION_TYPE))
+ if (key.equals(PREF_NOTIFICATION_TYPE) || key.equals(PREF_FOREGROUND_SERVICE))
updateNotification();
else if (key.equals(PREF_SYNC_ONLY_CHARGING) || key.equals(PREF_SYNC_ONLY_WIFI)
|| key.equals(PREF_SYNC_ONLY_WIFI_SSIDS))
@@ -464,6 +480,7 @@ public class SyncthingService extends Service implements
mApi.shutdown();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+ stopForeground(false);
nm.cancel(NOTIFICATION_ACTIVE);
for (FolderObserver ro : mObservers) {
diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml
index e674b36d..99508d5c 100644
--- a/src/main/res/values/strings.xml
+++ b/src/main/res/values/strings.xml
@@ -298,6 +298,10 @@ Please report any problems you encounter via Github.
Use this setting if you experience unexpected disconnects while operating on battery. This will result in increased battery consumption.
+ Run service with foreground priority
+
+ If enabled, Syncthing will run with foreground priority and is less likely to be stopped by Android. This might cause other services to be stopped if available memory is low.
+
Config was exported to %1$s
@@ -417,6 +421,8 @@ Please report any problems you encounter via Github.
Syncthing is running
+ Service is running with foreground priority.
+
Directory tree too deep. Check for cyclic symlinks
diff --git a/src/main/res/xml/app_settings.xml b/src/main/res/xml/app_settings.xml
index ea3265b0..ae4e22e4 100644
--- a/src/main/res/xml/app_settings.xml
+++ b/src/main/res/xml/app_settings.xml
@@ -162,6 +162,12 @@
android:summary="@string/keep_wakelock_while_binary_running_summary"
android:defaultValue="false" />
+
+