gpt4 book ai didi

android - 如何将更新的 long/lat 发送到 android 中的服务器?

转载 作者:行者123 更新时间:2023-11-30 04:11:40 26 4
gpt4 key购买 nike

在我的应用程序中,我想获取移动用户的更新位置,并且我想在一定时间的周期性间隔后或在用户移动一定距离(比如 500 米)后将其连续发送到服务器。我需要这些东西在后台完成。我知道为此我必须实现服务类。但我不知道该怎么做。我为此做了一些工作。谁能帮我解决这个问题。我在服务类中做了以下事情。

public class BackGroundService extends Service implements LocationListener{

public static final String Tag = BackGroundService.class.getName();
LocationManager myLocationManager;
Location myLocation;
LocationListener myLocationListener;


@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

public void OnCreate()
{
super.onCreate();
Log.d(Tag, "Service Started");

android.os.Debug.waitForDebugger();

Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_LOW);

String locationProvider = myLocationManager.getBestProvider(criteria, true);

myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000*60*5, 500, myLocationListener);
myLocation = myLocationManager.getLastKnownLocation(locationProvider);


}

public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
longitude = location.getLongitude();
latitude = location.getLatitude();

}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.d(Tag, "Provider is disabled");
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.d(Tag, "Location Provider is enabled");
}

public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}
}

从这里我想知道如何获取用户的当前纬度/经度并将其发送到服务器。

最佳答案

我的问题的答案是......

public class LocationService extends Service{

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
final LocationManager mlocmag = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
final LocationListener mlocList = new MyLocationList();
final Location loc = mlocmag.getLastKnownLocation(LocationManager.GPS_PROVIDER);
UpdateWithNewLocation(loc); // This method is used to get updated location.
mlocmag.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocList);
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
private void UpdateWithNewLocation(final Location loc) {
// TODO Auto-generated method stub

if(loc!= null)
{
final double lat =loc.getLatitude(); // Updated lat
final double Long = loc.getLongitude(); // Updated long


ConnectMySQL obj = new ConnectMySQL();
obj.call(lat,Long); // Call this method when location is updated and save the data.

}

else
{
String latLongStr = "No lat and longitude found";
Toast.makeText(this, "Your location is "+latLongStr ,Toast.LENGTH_LONG).show();
}


}
public class MyLocationList implements LocationListener
{

public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
UpdateWithNewLocation(arg0);
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"GPS Disable ", Toast.LENGTH_LONG).show();
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"GPS enabled", Toast.LENGTH_LONG).show();
}

public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

}

关于android - 如何将更新的 long/lat 发送到 android 中的服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10766749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com