Skip to content

Commit 5d39f1f

Browse files
brettchabotcopybara-androidxtest
authored andcommitted
Use Context.startForegroundService to launch SpeakEasyService on APIs >= 26.
Should fix intermittent 'app is in background uid' errors. PiperOrigin-RevId: 312384840
1 parent 8064ecd commit 5d39f1f

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

services/speakeasy/java/androidx/test/services/speakeasy/server/SpeakEasyContentProvider.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
import android.content.ContentProvider;
2020
import android.content.ContentValues;
21+
import android.content.Context;
2122
import android.content.Intent;
2223
import android.database.Cursor;
2324
import android.net.Uri;
25+
import android.os.Build;
2426
import android.os.Bundle;
2527

2628
/** Proxies the call method from the ContentProvider to the SpeakEasy service. */
@@ -63,7 +65,17 @@ public Bundle call(String unusedMethod, String unusedpackageName, Bundle extras)
6365
i.setClass(getContext(), SpeakEasyService.class);
6466
i.putExtras(extras);
6567

66-
getContext().startService(i);
68+
startForegroundService(getContext(), i);
6769
return new Bundle();
6870
}
71+
72+
// copy of ContentCompat.startForegroundService
73+
private static void startForegroundService(Context context, Intent intent) {
74+
if (Build.VERSION.SDK_INT >= 26) {
75+
context.startForegroundService(intent);
76+
} else {
77+
// Pre-O behavior.
78+
context.startService(intent);
79+
}
80+
}
6981
}

services/speakeasy/java/androidx/test/services/speakeasy/server/SpeakEasyService.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,17 @@ private static class DeathCallback implements SpeakEasy.BinderDeathCallback {
164164
public void binderDeath(String key, IBinder dead) {
165165
Intent msg = new Intent(context, SpeakEasyService.class);
166166
msg.putExtras(SpeakEasyProtocol.Remove.asBundle(key));
167-
context.startService(msg);
167+
startForegroundService(context, msg);
168+
}
169+
}
170+
171+
// copy of ContentCompat.startForegroundService
172+
private static void startForegroundService(Context context, Intent intent) {
173+
if (Build.VERSION.SDK_INT >= 26) {
174+
context.startForegroundService(intent);
175+
} else {
176+
// Pre-O behavior.
177+
context.startService(intent);
168178
}
169179
}
170180
}

0 commit comments

Comments
 (0)