简体   繁体   中英

Android: Call Method in another class

I have an activity called new_position, which i use to locate the current gps coordinates. I created a Listener class used in requestLocationUpdates. When my onLocationChanged method in my Listener class gets called i want to call a method in my new_position class. But it only seems to work when i make my class static, but i don't want to make it static. is there another way?

public class Listener implements LocationListener {

@Override
public void onLocationChanged(Location location) {        
    new_position.lati = location.getLatitude();
    new_position.longi = location.getLongitude();

    new_position.SaveCoordinates(new_position.str_posname);
}

It is as simple as below:

public class Listener implements LocationListener {

Context c;
public Listener(Context c)
{
    this.c=c;
}

@Override
public void onLocationChanged(Location location) {        
    ...

    ((new_position)c).SaveCoordinates(...);
}
}

initialize class object with mLocNetLis = new Listener(new_position.this);

Hope this helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM