jagzviruz
jagzviruz

Reputation: 1513

How to listen for GPS updated and receive values in MainActivity.java?

I am developing a prototype sample application. I have a GPS class in GPS.java file which implements LocationListener. I have a MainActivity.java file where I have an instance of GPS and I want to update the location into a text field. I have seen numerous examples where the Activity itself implements the OnLocationChanged such that its able to access the TextView fields. I, however, want to externalize the file. How can I do this? I am a newbie to Java. In javascript/AS3, I would broadcast an event and have a listener to identify and get the values. I am not completely sure how I can achieve this.

Upvotes: 0

Views: 470

Answers (4)

vipul mittal
vipul mittal

Reputation: 17401

You can do the same here as well for ex:

In you activity create a broadcast receiver as following:

public class MyReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
    <YourTextView>.setText(intent.getStringExtra("lat"));
  }
} 

Register this receiver in onCreate of activity with some custom intent filter:

MyReceiver mr=new MyReceiver ();
this.registerReceiver(mr,new IntentFilter("my-event"));

in onPause:

this.unregisterReceiver(mr);

Now in your GPS class in onLocationChanged callback just send a broadcast:

public void onLocationChanged(Location location) {
    Intent intent = new Intent();
    intent.putExtra("lat",location.getLatitude());
    intent.setAction("my-event");
    sendBroadcast(intent);
}

Upvotes: 0

Submersed
Submersed

Reputation: 8870

Pass a reference to a context in your GPS class (or better yet, implement it in a Service). Next, register a broadcast receiver in your MainActivity class on some custom action, for example com.mypackage.ACTION_RECEIVE_LOCATION.

In your GPS class's onLocationChanged(Location location) method, when you receive a location that fits your purposes, broadcast it in an intent as an extra.

Intent toBroadcast = new Intent(com.mypackage.ACTION_RECEIVE_LOCATION);
toBroadcast.putExtra(MainActivity.EXTRA_LOCATION,location);
context.sendBroadcast(toBroadcast);

In your MainActivity's registered receiver, receive the broadcast and handle the location.

public class MainActivity extends Activity {

public static final String EXTRA_LOCATION = "EXTRA_LOCATION";

    private class LocationUpdateReceiver extends BroadcastReceiver {

        /**
         * Receives broadcast from GPS class/service.
         */
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle extras = intent.getExtras();

            Location location = (Location) extras.get(MainActivity.EXTRA_LOCATION);

                //DO SOMETHING
               ...

  }
}

Upvotes: 1

Yogendra
Yogendra

Reputation: 5260

use location manager with listener in activity. its automatic update in this activity.

requestLocationUpdates(String, long, float, LocationListener);

http://developer.android.com/reference/android/location/LocationListener.html

i hope it will work.

Upvotes: 0

tyczj
tyczj

Reputation: 73721

Create an interface in your GPS class and then set the listener in your main activity to listen for callbacks. then when you location changes trigger that callback with the new location.

it would look something like this

GPS gps = new GPS();
gps.setLocationListener(new OnMyGpsLocationChanged(){
    @Override
    public void myLocationChanged(Location location){
        //use the new location here
    }
)}; 

the GPS class would have something like this

public interface OnMyGpsLocationChanged{
    public void myLocationChanged(Location location);
}

then when you location changed you would just do

listener.myLocationChanged(location);

in your onLocationChanged for your LocationManager

Upvotes: 1

Related Questions