diff options
| author | Blaster4385 <[email protected]> | 2026-06-24 08:56:58 +0530 |
|---|---|---|
| committer | Blaster4385 <[email protected]> | 2026-06-24 08:57:01 +0530 |
| commit | 145f96cf75e9dd84db87a728f816a009f673e361 (patch) | |
| tree | 8035e05893f39fd4f5553199550c4b9c9a9ef842 /app/src/main/java | |
| parent | d96215b4d11c044595398ad1f28090ab64f7582b (diff) | |
fix: start foreground service correctly to prevent crash
- Also fix mqtt spam if mqtt is disabled.
Diffstat (limited to 'app/src/main/java')
| -rw-r--r-- | app/src/main/java/dev/tablaster/dashpanel/DashPanelService.kt | 38 | ||||
| -rw-r--r-- | app/src/main/java/dev/tablaster/dashpanel/utils/NotificationUtils.kt | 13 |
2 files changed, 32 insertions, 19 deletions
diff --git a/app/src/main/java/dev/tablaster/dashpanel/DashPanelService.kt b/app/src/main/java/dev/tablaster/dashpanel/DashPanelService.kt index 06ef35c..fef7f4d 100644 --- a/app/src/main/java/dev/tablaster/dashpanel/DashPanelService.kt +++ b/app/src/main/java/dev/tablaster/dashpanel/DashPanelService.kt @@ -12,6 +12,7 @@ import android.util.Log import android.content.SharedPreferences import androidx.lifecycle.ProcessLifecycleOwner import androidx.preference.PreferenceManager +import androidx.preference.SwitchPreferenceCompat import dev.tablaster.dashpanel.mqtt.ConnectionData import dev.tablaster.dashpanel.mqtt.MQTTModule import dev.tablaster.dashpanel.mqtt.MQTTOptions @@ -104,28 +105,31 @@ class DashPanelService : Service(), MQTTModule.MQTTListener { wakeLock.setReferenceCounted(false) notificationUtils = NotificationUtils(this) - notificationUtils.startForegroundService() + notificationUtils.startForegroundService(this) Log.d(TAG, "DashPanelService started") val prefs = PreferenceManager.getDefaultSharedPreferences(this) - mqttOptions = MQTTOptions(prefs) - mqttModule = MQTTModule(this, mqttOptions, this) - - updateInterval = prefs.getString("sensor_update_interval", "15")!!.toInt() - - prefs.registerOnSharedPreferenceChangeListener(preferenceChangeListener) - - connectionData = ConnectionData(this) - connectionData.observe(ProcessLifecycleOwner.get()) { connected -> - if (connected && !isConnected) { - mqttModule.restart() - isConnected = true - } else if (!connected && isConnected) { - mqttModule.pause() - isConnected = false + val mqttEnabled = prefs.getBoolean("mqtt_enalbed", false) + mqttOptions = MQTTOptions(prefs) + mqttModule = MQTTModule(this, mqttOptions, this) + + updateInterval = prefs.getString("sensor_update_interval", "15")!!.toInt() + + prefs.registerOnSharedPreferenceChangeListener(preferenceChangeListener) + + connectionData = ConnectionData(this) + if (mqttEnabled) { + connectionData.observe(ProcessLifecycleOwner.get()) { connected -> + if (connected && !isConnected) { + mqttModule.restart() + isConnected = true + } else if (!connected && isConnected) { + mqttModule.pause() + isConnected = false + } } + mqttModule.start() } - mqttModule.start() if (!sensorsInitialized) { registerSensorProvider(BatterySensorProvider(this)) diff --git a/app/src/main/java/dev/tablaster/dashpanel/utils/NotificationUtils.kt b/app/src/main/java/dev/tablaster/dashpanel/utils/NotificationUtils.kt index 48baa85..4508df7 100644 --- a/app/src/main/java/dev/tablaster/dashpanel/utils/NotificationUtils.kt +++ b/app/src/main/java/dev/tablaster/dashpanel/utils/NotificationUtils.kt @@ -4,6 +4,7 @@ import android.app.Notification import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent +import android.app.Service import android.content.Context import android.content.Intent import android.os.Build @@ -56,12 +57,20 @@ class NotificationUtils(private val context: Context) { .build() } - fun startForegroundService() { + fun startForegroundService(service: Service) { val notification = buildNotification( context.getString(R.string.service_notification_title), context.getString(R.string.service_notification_message) ) - notificationManager.notify(NOTIFICATION_ID, notification) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + service.startForeground( + NOTIFICATION_ID, + notification, + android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC + ) + } else { + service.startForeground(NOTIFICATION_ID, notification) + } } fun updateNotification(title: String, message: String) { |