我有一个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();
>
最佳答案
要接收您的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);
这只是一个基本答案,您需要注意一些事项。
关于java - OnLocationChanged()不会自动调用。该位置未存储在数据库中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44931772/