Created
July 29, 2019 07:42
-
-
Save AlonsoFloo/41b3e6bb21a9985838a19816a98b51a9 to your computer and use it in GitHub Desktop.
ForegroundServiceLauncher is a helper for using better foreground functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ForegroundServiceLauncher(private val serviceClass: Class<out Service>) { | |
private var isStarting = false | |
private var shouldStop = false | |
@Suppress("DEPRECATION") | |
private inline fun <reified T : Service> Context.isServiceRunning(service: Class<out T>) = | |
(getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager) | |
.getRunningServices(Integer.MAX_VALUE) | |
.any { it.service.className == service.name } | |
@Synchronized | |
fun startService(context: Context, block: Intent.() -> Unit = {}) { | |
if (context.isServiceRunning(serviceClass)) { | |
isStarting = false | |
shouldStop = false | |
return | |
} | |
isStarting = true | |
shouldStop = false | |
ContextCompat.startForegroundService(context, Intent(context, serviceClass).apply { block() }) | |
} | |
@Synchronized | |
fun stopService(context: Context) { | |
if (isStarting) { | |
shouldStop = true | |
} else { | |
context.stopService(Intent(context, serviceClass)) | |
} | |
} | |
@Synchronized | |
fun onServiceCreated(service: Service) { | |
isStarting = false | |
if (shouldStop) { | |
shouldStop = false | |
service.stopSelf() | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestService: Service() { | |
private inner class LocalBinder : Binder() { | |
val instance: TestService | |
get() = this@TestService | |
} | |
companion object { | |
private val TAG = TestService::class.java.simpleName | |
private val LAUNCHER = ForegroundServiceLauncher(TestService::class.java) | |
private const val REQUEST_CODE = 1993 | |
@JvmStatic | |
fun start(context: Context) = LAUNCHER.startService(context) | |
@JvmStatic | |
fun stop(context: Context) = LAUNCHER.stopService(context) | |
} | |
override fun onBind(intent: Intent?): Binder = LocalBinder() | |
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | |
return START_STICKY | |
} | |
override fun onCreate() { | |
super.onCreate() | |
declareAsForeground() | |
} | |
private fun declareAsForeground() { | |
val notification = PendingIntent.getActivity( | |
this, | |
REQUEST_CODE, | |
Intent(this, SplashScreenActivity::class.java), | |
PendingIntent.FLAG_UPDATE_CURRENT | |
).let { | |
NotificationUtil.createNotification( | |
applicationContext, | |
it, | |
applicationContext.getString(R.string.notification_scan_title), | |
applicationContext.getString(R.string.notification_scan_message) | |
) | |
} | |
startForeground(REQUEST_CODE, notification) | |
LAUNCHER.onServiceCreated(this) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment