三星健康如何在没有长时间后台服务的情况下计算步数
how does samsung health count steps without long background service
我正在开发记录用户步数的健康应用程序,我正在寻找几个弱点如何在后台运行这个,但还没有解决方案
在不耗尽电池和系统资源的情况下长时间运行服务是我在编程中遇到的最困难的事情,今天我终于找到了实现我的想法的最佳解决方案
感谢 Coding in Flow channel
StepsService::class
class MainActivity() : FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
"background_service"
).setMethodCallHandler(
MethodCallHandler { call: MethodCall, result:
MethodChannel.Result ->
if ((call.method == "start")) {
//start Service.
val i: Intent = Intent(
getApplicationContext(),
StepsService::class.java
)
ContextCompat.startForegroundService(this, i)
}
result.success(1)
}
//start Service.
)
}
}
StepsService::class
/** The service is starting, due to a call to startService() */
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
Log.v("Service", "Start")
sensorManager = getSystemService(SENSOR_SERVICE) as SensorManager
stepCounterSensor = sensorManager!!.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
stepDetectorSensor = sensorManager!!.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR)
sensorManager!!.registerListener(this, stepCounterSensor, 0)
sensorManager!!.registerListener(this, stepDetectorSensor, 0)
//currentStepCount = 0;
currentStepsDetected = 0
stepCounter = 0
newStepCounter = 0
// --------------------------------------------------------------------------- \
// ___ (3) start handler ___ \
// remove any existing callbacks to the handler
handler.removeCallbacks(updateBroadcastData)
// call our handler with or without delay.
handler.post(updateBroadcastData) // 0 seconds
// ___________________________________________________________________________ \
val notificationIntent = Intent(
this,
MainActivity::class.java
)
val pendingIntent = PendingIntent.getActivity(
this,
0, notificationIntent, 0
)
val notification : Notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notification = Notification.Builder(this, channelId)
.setContentTitle("$currentStepsDetected Steps")
.setContentText("Target Steps 7500")
.setSmallIcon(R.mipmap.ic_notification)
.setContentIntent(pendingIntent)
.setVisibility(Notification.VISIBILITY_PRIVATE)
.build()
}else{
notification = Notification.Builder(this)
.setContentTitle("Example Service")
.setContentText("description")
.setSmallIcon(R.mipmap.ic_notification)
.setContentIntent(pendingIntent)
.setVisibility(Notification.VISIBILITY_PRIVATE)
.build()
}
startForeground(1, notification)
return START_NOT_STICKY
}
我正在开发记录用户步数的健康应用程序,我正在寻找几个弱点如何在后台运行这个,但还没有解决方案
在不耗尽电池和系统资源的情况下长时间运行服务是我在编程中遇到的最困难的事情,今天我终于找到了实现我的想法的最佳解决方案 感谢 Coding in Flow channel
StepsService::class
class MainActivity() : FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
"background_service"
).setMethodCallHandler(
MethodCallHandler { call: MethodCall, result:
MethodChannel.Result ->
if ((call.method == "start")) {
//start Service.
val i: Intent = Intent(
getApplicationContext(),
StepsService::class.java
)
ContextCompat.startForegroundService(this, i)
}
result.success(1)
}
//start Service.
)
}
}
StepsService::class
/** The service is starting, due to a call to startService() */
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
Log.v("Service", "Start")
sensorManager = getSystemService(SENSOR_SERVICE) as SensorManager
stepCounterSensor = sensorManager!!.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
stepDetectorSensor = sensorManager!!.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR)
sensorManager!!.registerListener(this, stepCounterSensor, 0)
sensorManager!!.registerListener(this, stepDetectorSensor, 0)
//currentStepCount = 0;
currentStepsDetected = 0
stepCounter = 0
newStepCounter = 0
// --------------------------------------------------------------------------- \
// ___ (3) start handler ___ \
// remove any existing callbacks to the handler
handler.removeCallbacks(updateBroadcastData)
// call our handler with or without delay.
handler.post(updateBroadcastData) // 0 seconds
// ___________________________________________________________________________ \
val notificationIntent = Intent(
this,
MainActivity::class.java
)
val pendingIntent = PendingIntent.getActivity(
this,
0, notificationIntent, 0
)
val notification : Notification;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notification = Notification.Builder(this, channelId)
.setContentTitle("$currentStepsDetected Steps")
.setContentText("Target Steps 7500")
.setSmallIcon(R.mipmap.ic_notification)
.setContentIntent(pendingIntent)
.setVisibility(Notification.VISIBILITY_PRIVATE)
.build()
}else{
notification = Notification.Builder(this)
.setContentTitle("Example Service")
.setContentText("description")
.setSmallIcon(R.mipmap.ic_notification)
.setContentIntent(pendingIntent)
.setVisibility(Notification.VISIBILITY_PRIVATE)
.build()
}
startForeground(1, notification)
return START_NOT_STICKY
}