123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package com.flacksta.chef.journeygpstracker
- import android.Manifest
- import android.app.*
- import android.content.Context
- import android.content.Intent
- import android.content.pm.PackageManager
- import android.graphics.Bitmap
- import android.graphics.BitmapFactory
- import android.os.IBinder
- import android.os.Looper
- import android.util.Log
- import androidx.core.app.ActivityCompat
- import androidx.core.app.NotificationCompat
- import androidx.core.content.ContextCompat
- import com.google.android.gms.location.*
- class TrackingService : Service() {
- private val mTag: String = "TrackingService"
- var counter = 0
- private lateinit var mFusedLocationClient: FusedLocationProviderClient
- private lateinit var mLocationRequest: LocationRequest
- private lateinit var mLocationCallback: LocationCallback
- override fun onCreate() {
- super.onCreate()
- Log.i(mTag,"onCreate()")
- generateForegroundNotification()
- startForeground(mNotificationId, notification)
- Log.i(mTag,"Requested foreground service...")
- }
- private fun requestLocationUpdates() {
- mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
- mLocationRequest = LocationRequest.create().apply {
- interval = 2000
- fastestInterval = 2000
- //smallestDisplacement = 2.0f
- priority = LocationRequest.PRIORITY_HIGH_ACCURACY
- }
- mLocationCallback = object : LocationCallback() {
- override fun onLocationResult(locationResult: LocationResult?) {
- locationResult ?: return
- for (location in locationResult.locations){
- counter += 1
- val acc = location.accuracy
- Log.i(mTag, "Got location: $counter $acc")
- }
- }
- }
- val permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
- if (permission == PackageManager.PERMISSION_GRANTED ) {
- mFusedLocationClient.requestLocationUpdates(mLocationRequest,
- mLocationCallback,
- Looper.getMainLooper())
- }
- }
- override fun onBind(intent: Intent): IBinder? {
- Log.i(mTag,"onBind()")
- return null
- }
- 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()")
- requestLocationUpdates()
- return START_STICKY
- }
- private var iconNotification: Bitmap? = null
- private var notification: Notification? = null
- private var mNotificationManager: NotificationManager? = null
- private val mNotificationId = 123
- private fun generateForegroundNotification() {
- 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
- }
- 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 = ContextCompat.getColor(this, R.color.purple_200)
- builder.build().also { notification = it }
- }
- override fun onDestroy() {
- super.onDestroy()
- Log.i(mTag,"onDestroy()")
- }
- }
|