不会自动调用 OnLocationChanged()。该位置未存储在数据库中
OnLocationChanged() is not automatically called. The location is not stored in the database
我有一个 gpsTracker java class,其中包含 onLocationChanged() 方法。我想 link 我的 MainActivity 的 OnCreate 函数使用这个 onLocationChanged() 方法。
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();
>
Class A{
Context ctx;
public A(Context ctx){
this.ctx=ctx;
}
//your rest code
}
在主要活动中:
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
希望对您有所帮助。
要在您的 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);
}
如果 Activity 关闭,请不要忘记注销接收器:
@Override
protected void onDestroy() {
if (mReceiver != null) {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
mReceiver = null;
}
super.onDestroy();
}
然后在 Utils
class 中的 OnLocationChange
上,在获得如下坐标后发送 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
class 的构造函数传递它,例如,如果您在 Activity
中创建一个实例:
MyUtils utils = new MyUtils(this);
这只是一个基本的答案,有些事情你必须要注意。
我有一个 gpsTracker java class,其中包含 onLocationChanged() 方法。我想 link 我的 MainActivity 的 OnCreate 函数使用这个 onLocationChanged() 方法。
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();
>
Class A{
Context ctx;
public A(Context ctx){
this.ctx=ctx;
}
//your rest code
}
在主要活动中: 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
希望对您有所帮助。
要在您的 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);
}
如果 Activity 关闭,请不要忘记注销接收器:
@Override
protected void onDestroy() {
if (mReceiver != null) {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
mReceiver = null;
}
super.onDestroy();
}
然后在 Utils
class 中的 OnLocationChange
上,在获得如下坐标后发送 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
class 的构造函数传递它,例如,如果您在 Activity
中创建一个实例:
MyUtils utils = new MyUtils(this);
这只是一个基本的答案,有些事情你必须要注意。