如何发送更新的经度/纬度到服务器的android? [英] How to send updated long/lat to server in android?

查看:111
本文介绍了如何发送更新的经度/纬度到服务器的android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我想获得移动用户的更新的位置,我想它之后的一定时间周期间隔或用户特定的行进后连续发送到服务器(说500米)distance.I需要这些东西将在backgroung.I完成知道为了这个,我必须实现服务类。但我没有得到确切怎么干这事做了一些工作。 任何人都可以请帮我在这个问题上。 我没有按照服务类的东西。

In my application i want to get the updated location of the mobile user and i want to send it to the server continuously after periodic interval of certain time or after the user travels certain(say 500 meter) distance.I need these things to be done in backgroung.I know for this i have to implement service class. But I am not getting exactly how to do this.I did some work on that. Can anybody please help me in this issue. I did following things in the service class.

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

    }
}

在这里,我想知道我怎么能得到当前的纬度/经度的用户,并将其发送给服务器。

From here I want know how can i get current lat/long of user and send it to the server.

推荐答案

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

Answer for my question is.......

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆