diff --git a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java index 5ffd9ec70..a0f8e9872 100644 --- a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java +++ b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java @@ -110,7 +110,7 @@ public class ApplicationDcContext extends DcContext { } if (!isScreenOn) { - context.startService(new Intent(context, KeepAliveService.class)); + KeepAliveService.startSelf(context); } } diff --git a/src/org/thoughtcrime/securesms/connect/KeepAliveService.java b/src/org/thoughtcrime/securesms/connect/KeepAliveService.java index 93d8c1813..c00f2e26b 100644 --- a/src/org/thoughtcrime/securesms/connect/KeepAliveService.java +++ b/src/org/thoughtcrime/securesms/connect/KeepAliveService.java @@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.connect; import android.app.Notification; import android.app.PendingIntent; import android.app.Service; +import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.IBinder; @@ -16,6 +17,23 @@ public class KeepAliveService extends Service { static KeepAliveService s_this = null; + public static void startSelf(Context context) + { + try { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // the started service has to call startForeground() within 5 seconds, + // see https://developer.android.com/about/versions/oreo/android-8.0-changes + context.startForegroundService(new Intent(context, KeepAliveService.class)); + } + else { + context.startService(new Intent(context, KeepAliveService.class)); + } + } + catch(Exception e) { + e.printStackTrace(); + } + } + @Override public void onCreate() { Log.i("DeltaChat", "*** KeepAliveService.onCreate()"); @@ -46,7 +64,7 @@ public class KeepAliveService extends Service { private void setSelfAsForeground() { stopForeground(true); - startForeground(FG_NOTIFICATION_ID, createNotification()); // TODO: if we target Android O, we should use startServiceInForeground() + startForeground(FG_NOTIFICATION_ID, createNotification()); } static public KeepAliveService getInstance() @@ -62,8 +80,6 @@ public class KeepAliveService extends Service { public static final int FG_NOTIFICATION_ID = 4142; private Notification createNotification() { - ApplicationDcContext dcContext = DcHelper.getContext(this); - Intent intent = new Intent(this, ConversationListActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // a notification _must_ contain a small icon, a title and a text, see https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Required diff --git a/src/org/thoughtcrime/securesms/connect/ScreenReceiver.java b/src/org/thoughtcrime/securesms/connect/ScreenReceiver.java index 81f7f98e6..1ecd947f4 100644 --- a/src/org/thoughtcrime/securesms/connect/ScreenReceiver.java +++ b/src/org/thoughtcrime/securesms/connect/ScreenReceiver.java @@ -15,7 +15,7 @@ public class ScreenReceiver extends BroadcastReceiver { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { Log.i("DeltaChat", "-------------------- Screen off --------------------"); dcContext.isScreenOn = false; - context.startService(new Intent(context, KeepAliveService.class)); + KeepAliveService.startSelf(context); } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { Log.i("DeltaChat", "-------------------- Screen on --------------------");