Question

I am currently writing an Android game app using GPS features of the device. I have come in contact with some problems. Here is my Player class

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

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

Here is my 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) {



}`

And here's my AsyncTask class

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

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


}

@Override
protected void onPreExecute()
{

}

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

}
`

I would like to get the GPS coordinates in the AsyncTask and use those coordinates to update the location of a circle (Player) in the View. How can I do that?

Was it helpful?

Solution

You can send the context (Activity that controls the view) to the AsyncTask:

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

Then in the onPostExecute method of the AsyncTask, you can call a method of the activity to update the view.

@Override
protected void onPostExecute(HashMap<String, String> result) {
    ((MyActivity) context).updateView(result);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top