通过AsyncTask更新视图

时间:2014-04-21 20:55:24

标签: android android-asynctask gps

我目前正在使用该设备的GPS功能编写Android游戏应用。我接触过一些问题。 这是我的Player类

public class Player {
private int xCor;
private int yCor;

public Player(int xCor, int yCor)
{
    this.xCor = xCor;
    this.yCor = yCor;
}
}

这是我的GameSurface View

public class GameSurface extends View{
private ArrayList<Ghost> ghostList;
private Paint paint;
public GameSurface(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

public GameSurface(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public GameSurface(Context context) {
    super(context);
    init();
}

public void init() {
    paint = new Paint();
    paint.setColor(0xff00ff00); // single int in hex (Green)
      // first ff = opaque;  first 00 = no red
      // second ff = green all the way on;  second 00 = no blue
      // could also do: paint.setColor(Color.GREEN);
    paint.setStyle(Style.STROKE);
    paint.setStrokeWidth(3);
}

@Override
public void onDraw(Canvas c) {



}`

这是我的AsyncTask类

public class UpdateLocation extends AsyncTask<GameSurface,GameSurface, Integer>{

@Override
protected Integer doInBackground(GameSurface... params) {


}

@Override
protected void onPreExecute()
{

}

@Override
protected void onProgressUpdate(GameSurface... params)
{

}
`

我想在AsyncTask中获取GPS坐标,并使用这些坐标更新视图中圆圈(播放器)的位置。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以将上下文(控制视图的Activity)发送到AsyncTask:

public class AsyncTask {
    protected void execute(Context context, HashMap<String, String>... params) {
    ...

然后在AsyncTask的onPostExecute方法中,您可以调用活动的方法来更新视图。

@Override
protected void onPostExecute(HashMap<String, String> result) {
    ((MyActivity) context).updateView(result);
}