-1

I made a WeatherSyncAdapterClass, I create a method and get the latitude and longitude value from GPS.

public void getGPS(){
    Context context = WeatherSyncAdapter.super.getContext();
    gps = new GpsTrack(context);
    if (gps.canGetLocation()) {
        while (lat == 0.0 && lon == 0.0) {
            setLat(gps.getLatitude());
            setLon(gps.getLongitude());
            latitude = Double.toString(lat);
            longitude = Double.toString(lon);
        }
    }
    else {
        gps.showSettingsAlert();
    }
}

...and then I set the URI value for the "locationsetting" with this statement:

    public void setLatLon(double a, double b) {
    //Double.toString(weather.getLatitude()+weather.getLongitude())
    LatLon = Double.toString(a+b);
}

(I just set the uri for the location setting from addition latitude and longitude ^_^, I know this is stupid)

My question is how to pass the "LatLon" value to the "mainActivity" for matching the URI? I know intent will pass the value but i think thats will be bad solution cause will (maybe) open the another activity.

please help me :(((

Bahadir Tasdemir
  • 10,325
  • 4
  • 49
  • 61
cahyowhy
  • 553
  • 2
  • 9
  • 26

1 Answers1

3
Intent intent = new Intent(current.this, mainActivity.class);
intent.putExtra("Lat",Double.toString(a));
intent.putExtra("Lon",Double.toString(b));
startActivity(intent);

Using the intents you can pass values to other activities. intent.putExtra method/function will send data in form of key,value pair.

In the MainActivity.class you can use following code to retrive the data from intent

Intent intent = getIntent();
String latitude = intent.getStringExtra("Lat");
String longitude = intent.getStringExtra("Lon");
Rakesh kumar
  • 392
  • 5
  • 13
  • yes, i wanna passing the value from the syncAdapter to the mainActivity, Intent its doesnt work cause i see "red" on "startActivity", what should i do :( – cahyowhy Sep 08 '16 at 14:17
  • you have to get the context reference and then try startActivity. You can use the following code to get the context reference getApplicationContext().startActivity(intent). If this does not help can you place the code so I can fix it. – Bharatha Aravind Sep 09 '16 at 06:15