我想在 SurfaceView 中显示 GPS 速度。
GPS 在 mainActivity-Class 中,因为我在 onCreate 之后需要它。有我的线程类,做循环。还有应该显示所有内容的 GamePanel-Class。
问题是,在 GamePanel 中,我尝试使用 textdraw 显示当前 GPS 速度,但它始终为 0。看起来它并没有真正运行 GPS 方法。这是为什么,有人可以帮忙吗?
public class MainActivity extends Activity implements LocationListener {
float nCurrentSpeed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new GamePanel(this));
LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
this.onLocationChanged(null);
}
@Override
public void onLocationChanged(Location location) {
if(location == null){
nCurrentSpeed = 1;
}
else {
nCurrentSpeed = location.getSpeed();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
public float getCurrentSpeed() {
return nCurrentSpeed;
}
}
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{
private MainThread thread;
private MainActivity mainActivity;
public GamePanel(Context context){
super(context);
//add the callback to the surfaceholder to intercept events
getHolder().addCallback(this);
thread = new MainThread(getHolder(), this);
setFocusable(true);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
boolean retry = true;
while(retry){
try {
thread.setRunning(false);
thread.join();
}catch(InterruptedException e){e.printStackTrace();}
retry = false;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder){
thread.setRunning(true);
thread.start();
mainActivity = new MainActivity();
}
public void update(){
}
public void drawText(Canvas canvas){
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setTextSize(100);
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
canvas.drawText("Speed: " + (mainActivity.getCurrentSpeed()), 10, 500, paint);
}
}
我没有显示类 Thread-Class,因为我猜它不相关。让我知道你是否需要看它。
对不起我的英语不好,我来自德国。
我希望能得到你的帮助!!!