English 中文(简体)
如何将更新的长/长发送到在机器人内部的服务器?
原标题:How to send updated long/lat to server in android?

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

    }
}

从这里,我想知道我怎样才能得到 目前的 Lat/ long 用户并把它发送到服务器上。

最佳回答

我问的答案是...

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 - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签