From ee8fdc90e6abefb2689e35fbbb8fb367f7318c6f Mon Sep 17 00:00:00 2001 From: Filip Strajnar Date: Sat, 19 Oct 2024 16:34:17 +0200 Subject: [PATCH] Service is now running in foreground. --- app/src/main/AndroidManifest.xml | 8 +++++++- .../logmylocation/LocationLoggingService.java | 20 +++++++++++++++++++ .../proculite/logmylocation/MainActivity.java | 2 +- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 720b42c..02a0988 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,10 @@ + + + + + android:exported="true" + android:foregroundServiceType="location" + /> = Build.VERSION_CODES.Q) { + NotificationChannel notificationChannel = new NotificationChannel( + "Foreground", + "Foreground", + NotificationManager.IMPORTANCE_DEFAULT + ); + NotificationManager notificationManager = getSystemService(NotificationManager.class); + notificationManager.createNotificationChannel(notificationChannel); + NotificationCompat.Builder builder = new NotificationCompat.Builder(this, notificationChannel.getId()); + builder.setSmallIcon(R.drawable.ic_launcher_foreground); + builder.setContentText("Started service in foreground."); + + ServiceCompat.startForeground(this,3784583, builder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION); + } + LocationDatabase database = Room .databaseBuilder(getApplicationContext(), LocationDatabase.class, "location") .fallbackToDestructiveMigration() diff --git a/app/src/main/java/com/proculite/logmylocation/MainActivity.java b/app/src/main/java/com/proculite/logmylocation/MainActivity.java index 5511f1d..a47be33 100644 --- a/app/src/main/java/com/proculite/logmylocation/MainActivity.java +++ b/app/src/main/java/com/proculite/logmylocation/MainActivity.java @@ -26,7 +26,7 @@ public class MainActivity extends AppCompatActivity implements LocationListener, super.onCreate(savedInstanceState); Intent serviceIntent = new Intent(this, LocationLoggingService.class); - startService(serviceIntent); + startForegroundService(serviceIntent); EdgeToEdge.enable(this); setContentView(R.layout.activity_main);