start foreground service according to android o

This commit is contained in:
B. Petersen
2019-02-08 00:58:16 +01:00
parent de28157ebb
commit ba5c190072
3 changed files with 21 additions and 5 deletions
@@ -110,7 +110,7 @@ public class ApplicationDcContext extends DcContext {
}
if (!isScreenOn) {
context.startService(new Intent(context, KeepAliveService.class));
KeepAliveService.startSelf(context);
}
}
@@ -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
@@ -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 --------------------");