|
| 1 | +package com.example.runningavater |
| 2 | + |
| 3 | +import android.app.NotificationChannel |
| 4 | +import android.app.NotificationManager |
| 5 | +import android.app.Service |
| 6 | +import android.content.Context |
| 7 | +import android.content.Intent |
| 8 | +import android.content.pm.ServiceInfo |
| 9 | +import android.hardware.Sensor |
| 10 | +import android.hardware.SensorEvent |
| 11 | +import android.hardware.SensorEventListener |
| 12 | +import android.hardware.SensorManager |
| 13 | +import android.os.Build |
| 14 | +import android.os.IBinder |
| 15 | +import androidx.core.app.NotificationCompat |
| 16 | +import androidx.core.app.ServiceCompat |
| 17 | +import com.example.runningavater.db.StepDate |
| 18 | +import kotlinx.coroutines.CoroutineScope |
| 19 | +import kotlinx.coroutines.Dispatchers |
| 20 | +import kotlinx.coroutines.SupervisorJob |
| 21 | +import kotlinx.coroutines.cancel |
| 22 | +import kotlinx.coroutines.launch |
| 23 | + |
| 24 | +class StepCounterService : Service() { |
| 25 | + private lateinit var sensorManager: SensorManager |
| 26 | + private var stepSensor: Sensor? = null |
| 27 | + private var totalSteps = 0 |
| 28 | + |
| 29 | + override fun onCreate() { |
| 30 | + super.onCreate() |
| 31 | + |
| 32 | +// sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager |
| 33 | +// stepSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER) |
| 34 | +// |
| 35 | +// if (stepSensor == null) { |
| 36 | +// Log.e("StepCounterService", "Step Counter sensor not available!") |
| 37 | +// stopSelf() |
| 38 | +// } |
| 39 | +// |
| 40 | +// |
| 41 | +// createNotificationChannel() |
| 42 | +// |
| 43 | +// |
| 44 | +// val notification = NotificationCompat.Builder(this, "step_service_channel") |
| 45 | +// .setContentTitle("Step Counter Service") |
| 46 | +// .setContentText("Counting your steps...") |
| 47 | +// .setSmallIcon(R.drawable.ic_steps) |
| 48 | +// .build() |
| 49 | +// |
| 50 | +// startForeground(1, notification) |
| 51 | + } |
| 52 | + |
| 53 | + val coroutineScope = CoroutineScope(SupervisorJob()) |
| 54 | + val walkcount = Walkcount(this, coroutineScope) |
| 55 | + |
| 56 | + override fun onStartCommand( |
| 57 | + intent: Intent?, |
| 58 | + flags: Int, |
| 59 | + startId: Int, |
| 60 | + ): Int { |
| 61 | + createNotificationChannel() |
| 62 | + val notification = |
| 63 | + NotificationCompat.Builder(this, "step_service_channel") |
| 64 | + .setContentTitle("Step Counter Service") |
| 65 | + .setContentText("Counting your steps...") |
| 66 | + .setSmallIcon(R.drawable.app_icon_yellow) |
| 67 | + .build() |
| 68 | + ServiceCompat.startForeground( |
| 69 | + this, |
| 70 | + 100, |
| 71 | + notification, |
| 72 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { |
| 73 | + ServiceInfo.FOREGROUND_SERVICE_TYPE_HEALTH |
| 74 | + } else { |
| 75 | + 0 |
| 76 | + }, |
| 77 | + ) |
| 78 | + startcount(this, walkcount) |
| 79 | +// stepSensor?.let { |
| 80 | +// sensorManager.registerListener(this, it, SensorManager.SENSOR_DELAY_NORMAL) |
| 81 | +// } |
| 82 | + return START_STICKY |
| 83 | + } |
| 84 | + |
| 85 | + override fun onDestroy() { |
| 86 | + super.onDestroy() |
| 87 | + stopcount(this, walkcount) |
| 88 | + // sensorManager.unregisterListener(this) |
| 89 | + coroutineScope.cancel() |
| 90 | + } |
| 91 | + |
| 92 | + override fun onBind(intent: Intent?): IBinder? = null |
| 93 | + |
| 94 | +// override fun onSensorChanged(event: SensorEvent?) { |
| 95 | +// if (event?.sensor?.type == Sensor.TYPE_STEP_COUNTER) { |
| 96 | +// val steps = event.values[0].toInt() |
| 97 | +// Log.d("StepCounterService", "Steps: $steps") |
| 98 | +// totalSteps = steps |
| 99 | +// } |
| 100 | +// } |
| 101 | +// |
| 102 | +// override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {} |
| 103 | + |
| 104 | + private fun createNotificationChannel() { |
| 105 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| 106 | + val channel = |
| 107 | + NotificationChannel( |
| 108 | + "step_service_channel", |
| 109 | + "Step Counter Service", |
| 110 | + NotificationManager.IMPORTANCE_LOW, |
| 111 | + ) |
| 112 | + val manager = getSystemService(NotificationManager::class.java) |
| 113 | + manager?.createNotificationChannel(channel) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +fun startcount( |
| 119 | + context: Context, |
| 120 | + walkcount: Walkcount, |
| 121 | +) { |
| 122 | + val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager |
| 123 | + val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR) |
| 124 | + sensorManager.registerListener(walkcount, sensor, SensorManager.SENSOR_DELAY_NORMAL) |
| 125 | +} |
| 126 | + |
| 127 | +class Walkcount(val context: Context, val coroutineScope: CoroutineScope) : SensorEventListener { |
| 128 | + override fun onSensorChanged(p0: SensorEvent?) { |
| 129 | + coroutineScope.launch(Dispatchers.IO) { |
| 130 | + val app = context.applicationContext as MainApplication |
| 131 | + app.db.stepDateDao().insertAll(StepDate(id = 0, System.currentTimeMillis())) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + override fun onAccuracyChanged( |
| 136 | + p0: Sensor?, |
| 137 | + p1: Int, |
| 138 | + ) { |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +fun stopcount( |
| 143 | + context: Context, |
| 144 | + walkcount: Walkcount, |
| 145 | +) { |
| 146 | + val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager |
| 147 | + sensorManager.unregisterListener(walkcount) |
| 148 | +} |
0 commit comments