英文:
How to make a stepcounter working in the background?
问题
这是您的代码,我会将其中的注释部分翻译成英文:
// 创建活动时调用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_DENIED){
// 请求权限
requestPermissions(new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, PHYISCAL_ACTIVITY);
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
textKroki= findViewById(R.id.textKroki);
sensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);
if(sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)!=null){
// 获取步数传感器
mLiczKrok = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
x= true;
}
else {
// 如果没有步数传感器
textKroki.setText("No sensor."); x=false;
}
}
// 传感器数值变化时调用
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor==mLiczKrok){
// 更新步数
LiczKroki= (int) sensorEvent.values[0];
textKroki.setText(String.valueOf(LiczKroki));
}
}
// 传感器精度变化时调用
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
// 恢复活动时调用
@Override
protected void onResume() {
super.onResume();
if(sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)!=null){
// 注册传感器监听器以继续计步
sensorManager.registerListener(this,mLiczKrok,SensorManager.SENSOR_DELAY_NORMAL);
}
}
// 暂停活动时调用
@Override
protected void onPause() {
super.onPause();
if(sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)!=null){
// 取消传感器监听器以节省资源
sensorManager.unregisterListener(this,mLiczKrok);
}
}
希望这可以帮助您理解代码中的注释部分。
英文:
I have an app which is supposed to count footsteps via my smartphone, it is working just fine. However, I would like it to be able to count steps while I'm not using it in the background.
This is my code, it is written in polish, for that I'm sorry. Here are few translations.
mLiczKrok is Footsteps count
textKroki is a text View which means TextFootsteps
What should I add in on pause method,in order to continue counting?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_DENIED){
//ask for permission
requestPermissions(new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, PHYISCAL_ACTIVITY);
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
textKroki= findViewById(R.id.textKroki);
sensorManager= (SensorManager) getSystemService(SENSOR_SERVICE);
if(sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)!=null){
mLiczKrok = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
x= true;
}
else {
textKroki.setText("Brak sensora."); x=false;
}
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (sensorEvent.sensor==mLiczKrok){
LiczKroki= (int) sensorEvent.values[0];
textKroki.setText(String.valueOf(LiczKroki));}}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
@Override
protected void onResume() {
super.onResume();
if(sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)!=null){
sensorManager.registerListener(this,mLiczKrok,SensorManager.SENSOR_DELAY_NORMAL);
}
}
@Override
protected void onPause() {
super.onPause();
if(sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)!=null){
sensorManager.unregisterListener(this,mLiczKrok);
}
}
答案1
得分: 0
在暂停方法中似乎正在取消注册 type_step_counter 传感器。这将永远不会接收到脚步计数。请删除在 onpause 方法中的以下行,并重新运行和测试您的应用程序。
将上述行添加到 activity 的 ondestroy() 方法中。
注意:如果这个上下文不适合您,请通知。在您的项目中是否有在 onpause 方法中取消注册脚步计数传感器的原因?
英文:
it seems that on pause method having unregistering type_step_counter sensor. Which will never recieves the foot step counting Please remove following lines present in onpause method. And re-run and test your app.
if(sensorManager.getDefaultSensor(
Sensor.TYPE_STEP_COUNTER) != null){
sensorManager.unregisterListener(this, mLiczKrok);
}
Add above lines inside ondestroy() of activity.
Note: if this context Is not suitable to you, please inform. In you project is there any reason behind unregistering foot step counter sensor in onpause method .??
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论