gpt4 book ai didi

java - 使用 AsyncTask 作为内部类

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:56 25 4
gpt4 key购买 nike

我创建了一个计步器应用程序,它作为独立应用程序运行良好。我认为我可以在我拥有的另一个应用程序中使用它,因此了解 AsyncTask 类来执行相同的操作。我阅读了 AsyncTask 的语法,并相应地将计步器代码粘贴到其中,但它给了我无法修复的错误。所以我刚刚创建了一个新项目,并将 AsyncTask 代码粘贴到那里,以找出我到底错在哪里,但我没有得到任何结果。请帮忙。

这是我的代码:

package com.example.momo.background;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

public TextView count;
public TextView mod;
public TextView cTime;
public TextView pTime;
public TextView timeDiff;
public TextView stepSpeed;
public SensorManager sensorManager;

public float speed;
float fin, initial, tdiff, sSpeed;
public SensorEvent event;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new backgroundtask().execute();


}

@Override
public void onSensorChanged(SensorEvent event) {
Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if (countSensor != null) {

sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI); // original
new backgroundtask().execute();

} else {
Toast.makeText(this, "count sensor not available!", Toast.LENGTH_LONG).show(); //toast prints the message
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

class backgroundtask extends AsyncTask<Void, Float, Float> {

protected void onPreExecute() {
}

protected Float doInBackground(Void... params) {


fin = initial / 1000000000;
initial = event.timestamp;
tdiff = initial / 1000000000 - fin;
sSpeed = 1 /tdiff;
speed = sSpeed;
publishProgress(speed);
return speed;

}

@Override
protected void onProgressUpdate(Float... values) {
pTime.setText(String.valueOf(fin));
cTime.setText(String.valueOf(initial / 1000000000));
count.setText(String.valueOf(event.values[0])); // normal pedometer
mod.setText(String.valueOf((initial / 1000000000) % 5));
timeDiff.setText(String.valueOf(tdiff));
stepSpeed.setText(String.valueOf(values));
}

protected void onPostExecute(Void unused)
{

}
}

}

最佳答案

据我了解,您对异步类的实现不正确。您向我们展示了堆栈跟踪或解释了错误,这也可能会有所帮助将代码从

移出
@Override
protected void onProgressUpdate(Float... values) {

}

protected void onPostExecute(Float speed)
{
pTime.setText(String.valueOf(fin));
cTime.setText(String.valueOf(initial / 1000000000));
count.setText(String.valueOf(event.values[0])); // normal pedometer
mod.setText(String.valueOf((initial / 1000000000) % 5));
timeDiff.setText(String.valueOf(tdiff));
stepSpeed.setText(String.valueOf(speed));
}

每次调用 onSensorChanged(SensorEvent event) 方法时,它都会创建一个新的 AsyncTask。

关于java - 使用 AsyncTask 作为内部类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34250748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com