1

I have a gpsTracker java class which contains the onLocationChanged() method. I want to link my MainActivity's OnCreate function with this onLocationChanged() method.

public void onLocationChanged(Location newlocation) {

    Log.d("latitude", "old" + Double.toString(this.location.getLatitude()));
    Log.d("Longitude", "old " + 
     Double.toString(this.location.getLatitude()));
    Log.i("info","LOCATION CHANGED");
    Log.d("latitude", "NEW: CHANGED TO KOLKATA" + 
    Double.toString(newlocation.getLatitude()));
    Log.d("Longitude", "NEW: CHANGED TO KOLKATA " + 
     Double.toString(newlocation.getLatitude()));
    this.location = newlocation;
    JSONObject locationChange = new JSONObject();
    getLocationName(location.getLatitude(), location.getLongitude());
    try {
        locationChange.put("userid", 
       PreferenceManager.getInstance().getUserId());
        locationChange.put("location", addressString);
        locationChange.put("connection", 
        PreferenceManager.getInstance().getConnectAllow() ? "1" : "0");
        locationChange.put("connection", "0");
        locationChange.put("notification", 
    PreferenceManager.getInstance().getPushNotiAllow() ? "0" : "1");

        Log.d(TAG, "LOCATION CHANGED BY GPS FUNCTION: " + locationChange);

        RequestHandler.getInstance().postWithHeader(null, 
      getString(R.string.baseurl) + getString(R.string.settings), 
      locationChange, this, 
    AppConstants.ApiRequestCodeKey.SAVE_APP_SETTINGS);
     }catch (JSONException e) {
                      e.printStackTrace();
         }
  }
public void getLocationName(double latitude, double longitude) {
    Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
    try {
        addresses = geocoder.getFromLocation(latitude, longitude, 1);
        addressString = addresses.get(0).getAddressLine(0)
                + addresses.get(0).getAddressLine(1) + " ";
    } catch (IOException e) {
        e.printStackTrace();
    }
}>

The onCreate function in the MainActivity() contains this.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_home_frame);

utils.mixpanelTrack(this, "Reached Home Page");
 homeFragObj = this;
    initViews();
    initSetup();
    initListeners();
    loadData();
>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Faiza Ahmad
  • 57
  • 1
  • 7

2 Answers2

0
Class A{
 Context ctx;
 public A(Context ctx){
  this.ctx=ctx;
 }
 //your rest code
}

In MainActivity: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.activity_home_frame);

utils.mixpanelTrack(this, "Reached Home Page");
 homeFragObj = this;
 initViews();
 initSetup();
 initListeners();
 loadData();
 A a=new A(this);//Making an Obj
 a.onLocationChanged(location);//your method invocation

Hope this helps.

Sachin Bahukhandi
  • 2,378
  • 20
  • 29
0

To receive every location change in your Activiy, it´s the best option to use Broadcasts. Create a instance variable of BroadcastReceiver:

BroadcastReceiver mReceiver;

In onCreate() add this receiver:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.activity_home_frame);

  IntentFilter filter = new IntentFilter();
  filter.addAction("LOCATION_CHANGE"); 

  mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

      String latitude = intent.getExtras().getString("LATITUDE");
      String longitude= intent.getExtras().getString("LONGITUDE");
    }
  };
    LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
}

Don´t forget to unregister the receiver if Activity closes:

 @Override
 protected void onDestroy() {
  if (mReceiver != null) {
   LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
   mReceiver = null;
  }
  super.onDestroy();
 }

And on OnLocationChange in your Utils class, send the Broadcast after you have the coordinates like this:

Intent intent=new Intent(context,YourActivity.class);
intent.setAction("LOCATION_CHANGE");
intent.putExtra("LATITUDE",Double.toString(latitude));
intent.putExtra("LONGITUDE",Double.toString(longitude));
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

What you need is the context that you can pass via your constructor of your Utils class, for example, if you create an instance in your Activity:

MyUtils utils = new MyUtils(this);

This is just a basic answer, there are some things you have to be aware of.

Opiatefuchs
  • 9,800
  • 2
  • 36
  • 49
  • Thank you so much for sharing your knowledge. This indeed looks like a great solution. Thank you! I just read more about Broadcast Receivers and it sounds like an interesting concept. I'll implement this in my code and keep you updated. :) – Faiza Ahmad Jul 05 '17 at 18:19