Skip to content

Instantly share code, notes, and snippets.

@webserveis
Created February 26, 2020 12:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webserveis/f4639c2d46c2dd6dc6c57a811636f05a to your computer and use it in GitHub Desktop.
Save webserveis/f4639c2d46c2dd6dc6c57a811636f05a to your computer and use it in GitHub Desktop.
Foreground Services en Kotlin

Ejemplo de servicios Foreground en Kotlin

Preparativos

Añadir en AndroidManifest.xml

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

Y la declaración del servicio

<service
    android:name=".MyService"
    android:enabled="true"
    android:exported="false" />

Añadir los iconos correspondientes play y stop

Kotlin extension

Añadir la extensión de funcionalidad kotlin

fun Context?.toast(text: CharSequence, duration: Int = Toast.LENGTH_LONG) = this?.let { Toast.makeText(it, text, duration).show() }
fun Context?.toast(@StringRes textId: Int, duration: Int = Toast.LENGTH_LONG) = this?.let { Toast.makeText(it, textId, duration).show() }

@Suppress("DEPRECATION")
fun <T> Context.isServiceRunning(service: Class<T>): Boolean {
    return (getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager)
        .getRunningServices(Integer.MAX_VALUE)
        .any { it.service.className == service.name }
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@drawable/ic_play_arrow_24dp" />
<include layout="@layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
package com.webserveis.testservices
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
if (isServiceRunning(MyService::class.java)) {
toast(R.string.service_running)
fab.setImageResource(R.drawable.ic_stop_24dp)
} else {
fab.setImageResource(R.drawable.ic_play_arrow_24dp)
}
fab.setOnClickListener { view ->
//Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show()
if (!isServiceRunning(MyService::class.java)) {
MyService.startService(this, getText(R.string.service_summary).toString())
toast(R.string.service_start)
fab.setImageResource(R.drawable.ic_stop_24dp);
} else {
MyService.stopService(this)
toast(R.string.service_stop)
fab.setImageResource(R.drawable.ic_play_arrow_24dp)
}
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_service -> {
if (isServiceRunning(MyService::class.java)) {
toast(R.string.service_running)
} else {
toast(R.string.service_not_running)
}
true
}
else -> super.onOptionsItemSelected(item)
}
}
}
package com.webserveis.testservices
import android.app.*
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
class MyService : Service() {
companion object {
const val CHANNEL_ID = "ForegroundService Kotlin"
fun startService(context: Context, message: String) {
val startIntent = Intent(context, MyService::class.java)
startIntent.putExtra("inputExtra", message)
ContextCompat.startForegroundService(context, startIntent)
}
fun stopService(context: Context) {
val stopIntent = Intent(context, MyService::class.java)
context.stopService(stopIntent)
}
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
//do heavy work on a background thread
val input: String? = intent?.getStringExtra("inputExtra")
createNotificationChannel()
val notificationIntent = Intent(this, MainActivity::class.java)
val pendingIntent: PendingIntent = PendingIntent.getActivity(
this,
0, notificationIntent, 0
)
val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(getText(R.string.service_title))
.setContentText(input)
.setSmallIcon(R.drawable.ic_android_24dp)
.setContentIntent(pendingIntent)
.build()
startForeground(1, notification)
return START_NOT_STICKY
}
override fun onBind(intent: Intent): IBinder? {
return null
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val serviceChannel = NotificationChannel(
CHANNEL_ID, "Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
val manager: NotificationManager? = getSystemService(NotificationManager::class.java)
manager!!.createNotificationChannel(serviceChannel)
}
}
}
<string name="action_service_check">Check service</string>
<string name="service_title">Example service</string>
<string name="service_summary">This is a foreground service Notification</string>
<string name="service_start">Service start</string>
<string name="service_stop">Service stop</string>
<string name="service_running">Service is running</string>
<string name="service_not_running">Service is not running</string>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment