不会自动调用OnLocationChanged()。该位置未存储在数据库中

时间:2017-07-05 16:41:07

标签: java android location on-location-changed

我有一个gpsTracker java类,它包含onLocationChanged()方法。我想用我的onLocationChanged()方法链接我的MainActivity的OnCreate函数。

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();
    }
}>

MainActivity()中的onCreate函数包含此函数。

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();
>

2 个答案:

答案 0 :(得分:0)

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

在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

希望这有帮助。

答案 1 :(得分:0)

要在Activiy中接收每个位置更改,这是使用Broadcasts的最佳选择。创建BroadcastReceiver的实例变量:

BroadcastReceiver mReceiver;

onCreate()中添加此接收器:

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);
}

如果活动关闭,请不要忘记取消注册接收者:

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

OnLocationChange课程的Utils上,在您拥有如下坐标后发送Broadcast

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);

您需要的是context,您可以通过Utils课程的构造函数传递,例如,如果您在Activity中创建了一个实例:

MyUtils utils = new MyUtils(this);

这只是一个基本答案,有些事情你必须要注意。