Sfoglia il codice sorgente

Foreground service is not crashing at least.

Thomas Chef 3 anni fa
parent
commit
b473072bf1

+ 2 - 0
app/src/main/AndroidManifest.xml

@@ -5,6 +5,8 @@
 
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
+    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
+
 
     <application
         android:allowBackup="true"

+ 1 - 1
app/src/main/java/com/flacksta/chef/journeygpstracker/MainScreenActivity.kt

@@ -98,7 +98,7 @@ class MainScreenActivity : AppCompatActivity() {
         startLocationUpdates()*/
 
         Intent(this, TrackingService::class.java).also { intent ->
-            startService(intent)
+            startForegroundService(intent)
         }
 
     }

+ 53 - 1
app/src/main/java/com/flacksta/chef/journeygpstracker/TrackingService.kt

@@ -1,9 +1,14 @@
 package com.flacksta.chef.journeygpstracker
 
-import android.app.Service
+import android.app.*
+import android.content.Context
 import android.content.Intent
+import android.graphics.Bitmap
+import android.graphics.BitmapFactory
+import android.os.Build
 import android.os.IBinder
 import android.util.Log
+import androidx.core.app.NotificationCompat
 
 class TrackingService : Service() {
 
@@ -22,9 +27,56 @@ class TrackingService : Service() {
     override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
         @Suppress("UNUSED_VARIABLE") val retVal: Int = super.onStartCommand(intent, flags, startId)
         Log.i(mTag,"onStartCommand()")
+        generateForegroundNotification()
         return START_STICKY
     }
 
+    private var iconNotification: Bitmap? = null
+    private var notification: Notification? = null
+    var mNotificationManager: NotificationManager? = null
+    private val mNotificationId = 123
+
+    private fun generateForegroundNotification() {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            val intentMainLanding = Intent(this, MainScreenActivity::class.java)
+            val pendingIntent =
+                    PendingIntent.getActivity(this, 0, intentMainLanding, PendingIntent.FLAG_IMMUTABLE)
+            iconNotification = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
+            if (mNotificationManager == null) {
+                mNotificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
+            }
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+                assert(mNotificationManager != null)
+                mNotificationManager?.createNotificationChannelGroup(
+                        NotificationChannelGroup("chats_group", "Chats")
+                )
+                val notificationChannel =
+                        NotificationChannel("service_channel", "Service Notifications",
+                                NotificationManager.IMPORTANCE_MIN)
+                notificationChannel.enableLights(false)
+                notificationChannel.lockscreenVisibility = Notification.VISIBILITY_SECRET
+                mNotificationManager?.createNotificationChannel(notificationChannel)
+            }
+            val builder = NotificationCompat.Builder(this, "service_channel")
+
+            builder.setContentTitle(StringBuilder(resources.getString(R.string.app_name)).append(" service is running").toString())
+                    .setTicker(StringBuilder(resources.getString(R.string.app_name)).append("service is running").toString())
+                    .setContentText("Touch to open") //                    , swipe down for more options.
+                    .setSmallIcon(R.drawable.ic_baseline_map_24)
+                    .setPriority(NotificationCompat.PRIORITY_LOW)
+                    .setWhen(0)
+                    .setOnlyAlertOnce(true)
+                    .setContentIntent(pendingIntent)
+                    .setOngoing(true)
+            if (iconNotification != null) {
+                builder.setLargeIcon(Bitmap.createScaledBitmap(iconNotification!!, 128, 128, false))
+            }
+            builder.color = resources.getColor(R.color.purple_200)
+            notification = builder.build()
+            startForeground(mNotificationId, notification)
+        }
+    }
+
     override fun onDestroy() {
         super.onDestroy()
         Log.i(mTag,"onDestroy()")