-1

referring to the code below.. I got a class LocationService and have a variable latitude in it. I want use that variable in another class Post. How do I do that. Basically I want get the latitude and longitude which i get from there in the other class also.. As I want to post it on the server. So how do i do that.

LocationService.java

public class LocationService extends Service{

Context context;
WakeLock wl;


@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}


public int onStartCommand(Intent intent, int flags, int startId) {
    PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "lock");
    wl.acquire();
    context = this;
    final String who = intent.getStringExtra("who");
    final LocationManager locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    final LocationListener listener = new LocationListener(){

        // start location changed

        public void onLocationChanged(Location loc) {
            double latitude = loc.getLatitude();
            double longitude = loc.getLongitude();
                                    ......
                                    ......
                                    }
Post.java

public class Post extends LocationService {

}

EDITED Post.java

public class Post extends LocationService {{

super.latitude = loc.getLatitude();


 }
 }

Will this get me the latitude from LocationService class to Post class..?

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
  • how do you use the `Post` class? Is it another different Service? – zapl Apr 24 '12 at 10:14
  • yah!! a different service.. I want to get the latitudes and longitudes from class LocationService to the class Post and then in class will help me post the latitudes and longitudes on a server – Radha Kumar Apr 24 '12 at 10:16
  • Take `latitude` and `longitude` variables outside the `LocationListener` and make them the direct members of `LocationService` class. That is, put them where you've declared `context` and `wl` – Vishnu Haridas Apr 24 '12 at 10:16

3 Answers3

1

Define some protected variables in LocationService then Post can see them. Or write getter methods in LocationService and call them from Post with super.getLat

Flynny75
  • 1,593
  • 1
  • 11
  • 11
0

Your OOPS Concepts are not Clear. Go HERE

You will have to make those variables Public as you want them to be used in other Classes and declare it where Context is declared. Then you can make an object of the class and use it. Get the Fundamentals Clear.

Bhavin
  • 6,020
  • 4
  • 24
  • 26
0

I had a same problem, see question and answer here: Android: pass parameter to Service from Activity

In few words, you need to create an interface that declare all method signature that you want to call from a Activity, implement its methods in your LocalBinder and then reference to this interface from your Activity.

Community
  • 1
  • 1
Ilya Blokh
  • 11,923
  • 11
  • 52
  • 84