feat: turned ReceiverService into foreground service

This commit is contained in:
Timo Ley 2024-07-01 19:16:49 +02:00
parent e954829595
commit 378e6f030d
2 changed files with 9 additions and 1 deletions

View file

@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
@ -33,7 +35,7 @@
<action android:name="com.example.broadcasts.action.CUSTOM_BROADCAST" />
</intent-filter>
</receiver>
<service android:name=".ReceiverService" />
<service android:name=".ReceiverService" android:foregroundServiceType="specialUse"/>
<receiver android:name=".AlarmReceiver" />
</application>
</manifest>

View file

@ -4,6 +4,7 @@ import android.app.Service
import android.content.Intent
import android.content.IntentFilter
import android.os.IBinder
import androidx.core.app.NotificationCompat
class ReceiverService: Service() {
@ -15,6 +16,11 @@ class ReceiverService: Service() {
override fun onCreate() {
super.onCreate()
val notification = NotificationCompat.Builder(this, "default_notifications")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("Listening for Airplanemode change")
.build()
startForeground(2, notification)
registerReceiver(
airplaneModeChangedReceiver,
IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)