Skip to content

Commit

Permalink
Use Context.startForegroundService to launch SpeakEasyService on APIs…
Browse files Browse the repository at this point in the history
… >= 26.

Should fix intermittent 'app is in background uid' errors.

PiperOrigin-RevId: 312384840
  • Loading branch information
brettchabot authored and copybara-androidxtest committed May 20, 2020
1 parent 8064ecd commit 5d39f1f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

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

getContext().startService(i);
startForegroundService(getContext(), i);
return new Bundle();
}

// copy of ContentCompat.startForegroundService
private static void startForegroundService(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= 26) {
context.startForegroundService(intent);
} else {
// Pre-O behavior.
context.startService(intent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,17 @@ private static class DeathCallback implements SpeakEasy.BinderDeathCallback {
public void binderDeath(String key, IBinder dead) {
Intent msg = new Intent(context, SpeakEasyService.class);
msg.putExtras(SpeakEasyProtocol.Remove.asBundle(key));
context.startService(msg);
startForegroundService(context, msg);
}
}

// copy of ContentCompat.startForegroundService
private static void startForegroundService(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= 26) {
context.startForegroundService(intent);
} else {
// Pre-O behavior.
context.startService(intent);
}
}
}

0 comments on commit 5d39f1f

Please sign in to comment.