user1277518
user1277518

Reputation: 1

LocationListener and updates to webservice in an interval of time as background proocess

My Andoid application contains many activities. I want to run a background process that is independent of these activities (no matter whichever activity is running on UI). In this background process, I want to capture Location Updates using LocationListener and want to call web service to store the captured location in the database. And this calling of webservice has to happen only in 2 min interval. Can someone please help me in implementing this.

Thanks. Android developer

Upvotes: 0

Views: 512

Answers (2)

Sandip Jadhav
Sandip Jadhav

Reputation: 7165

Check this

public class LocationUpdator extends Service{
    Location location;
    private int normallocationwait = 2000;
        Timer timer = new Timer();
        private final Handler handler = new Handler();
    Intent intent;

    public void  onCreate(){
        super.onCreate();
        intent = new Intent(BROADCAST_ACTION);  
        updateNotification();
    }

    //int onStartCommand(Intent intent, int flags, int startId) 
    public void onStart(Intent intent,int startId){
        super.onStart(intent, startId);
        handler.removeCallbacks(sendUpdatesToUI);
            handler.postDelayed(sendUpdatesToUI, 1000); // 1 second
        Log.v("Location Servics", "Start Service");
    }

    public void onDestroy(){
        super.onDestroy();
        Log.v("Location Servics", "Destroy Service");
    }


    protected void updateNotification() {
             LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
             LocationListener locationListener = new MyLocationlistener();

             lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, normallocationwait, 0.250f, locationListener);
             location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }

    private class MyLocationlistener implements LocationListener {

            public void onLocationChanged(Location location){
                 if(location!=null){
                           dumpLocation(location);
             } 
            }

         public void onProviderDisabled(String provider){
             Log.v("Loc Update","\nProvider disabled: " + provider);
         }

         public void onProviderEnabled(String provider){
             Log.v("Loc Update","\nProvider enabled: " + provider);
         }

         public void onStatusChanged(String provider, int status, Bundle extras){
             Log.v("Loc Update","\nProvider status changed: " + provider + ", status="
                        + status + ", extras=" + extras);
         }

         private void dumpLocation(Location location) {
                         Log.v("Loc Update","\n" + location.toString());
                     Log.v("Demo", location.toString());
                     if(GlobalValues.whereRegister == 1){
                             String url  = TaxiUrls.taxiLocationUpdate_url  + "taxi_device_id="+ GlobalValues.deviceID +
                                    "&lat="+ location.getLatitude() + "&lng=" + location.getLongitude();
                             Toast.makeText(getBaseContext(), "Location Update", Toast.LENGTH_SHORT).show();
                             HttpClient httpclient = new DefaultHttpClient();
                     HttpPost httppost = new HttpPost(url);
                     try {
                        HttpResponse response = httpclient.execute(httppost);
                            Log.v("Message", response.toString());
                     } catch (ClientProtocolException e) {
                        Log.e("Sending Message",e.getMessage().toString());
                     } catch (IOException e) {
                        Log.e("Sending Message",e.getMessage().toString());
                     }
                } 
            }

     }

}

Upvotes: 5

SimpleSi
SimpleSi

Reputation: 833

Maybe worth launching a simple AsyncTask to do this in the background.

Upvotes: 0

Related Questions