Alright, so here’s what I would try first, if you want an optional foreground service.
Setup a boolean preference.
I would set up a Service, call it TagTimeService.java. Remember to declare it in the AndroidManifest.xml. While you’re in there, don’t forget to ask for the
Then, in the onCreate of your launch activity, I would do something like the following, after loading preferences, and checking that the preference is set to launch the foreground service:
Intent launch = new Intent( sContext, TagTimeService.class );
For newer Androids, you have to use notification channels, so set that up. You can set that up idempotently, so let’s do that, in the onCreate of your TagTimeService:
NotificationChannel fgChannel = new NotificationChannel(FOREGROUND_CHANNEL_ID,
fgChannel.setDescription("A persistent notification helping remind both you and Android about TagTime. Feel free to hide, it will still do its job hidden :)");
NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE );
And then actually show the notification…
Intent notificationIntent = new Intent(fooContext, WhereShouldTheNotificationGoIfTappedBytheUser.class);
PendingIntent pendingIntent = PendingIntent.getActivity(fooContext,
Notification notification = new NotificationCompat.Builder(this, FOREGROUND_CHANNEL_ID)
if (LOCAL_LOGV) Log.v(TAG, "runAsForeground: created persistent notification.");
I am experimenting launching this as a collapsed notification, but I haven’t gotten to that quite yet.
You may want to wrap that all in a “If on O or newer” conditional.
Let me know how it goes, but I am still wrapping up a large offline project and will have limited availability. This should get you most of the way!