Tuesday, May 17, 2011

update Current location

package com.trackme;

import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONObject;

import com.examples.LoginActivity;
import com.examples.R;



import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.util.Log;
import android.widget.Toast;

public class mapservice extends Service
{

    static Location curLocation;
    static Boolean locationChanged;
    static LocationManager locMan;
   
    LocationListener gpsListener = new LocationListener()
    {
        public void onLocationChanged(Location location)
        {
            // Log.w("GPS", "Started");
            if (curLocation == null)
            {
                curLocation = location;
                locationChanged = true;
            }

            if (curLocation.getLatitude() == location.getLatitude()
                    && curLocation.getLongitude() == location.getLongitude())
                locationChanged = false;
            else
                locationChanged = true;

            curLocation = location;

            /*Toast.makeText(
                    getBaseContext(),
                    "Lat : " + String.valueOf(curLocation.getLatitude())
                            + "\n Long : "
                            + String.valueOf(curLocation.getLongitude()),
                    Toast.LENGTH_LONG).show();*/
           
            updateLocation(location.getLatitude(),location.getLongitude());
           
           
            /*
             * if (locationChanged) locMan.removeUpdates(gpsListener);
             */

        }

        public void onProviderDisabled(String provider) {

        }

        public void onProviderEnabled(String provider) {
            // Log.w("GPS", "Location changed", null);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            if (status == 0)// UnAvailable
            {

            } else if (status == 1)// Trying to Connect
            {

            } else if (status == 2) {// Available

            }
        }

    };
    private static final long UPDATE_INTERVAL = 5000;

    private Timer timer = new Timer();
   

    //"http://java.greymatterindia.com/montacts/web/index.php/api/updateLatLng?data={"TokenKey":"ADZWYA==","Userlng":"-122.030731","Userlat":"37.331688","UsrId":234}
   
    public void updateLocation(double lat ,double lng)
    {

        /*System.out.println("update================");
        Toast.makeText(getApplicationContext(),
                "lat==" + lat + "longi==" + lng, Toast.LENGTH_LONG).show();*/

       
        String deviceid=LoginActivity.userdevice_id;
        String data ="http://imt.6te.net/Webservice_7s7/log.php?device_id="+deviceid+"&lattitude="
                + lat+ "&longitude="+lng+"";

        JSONObject json = com.json.RestJsonClient.connect(data.trim());

        System.out.println("url response=============" + json);

        // Toast.makeText(context, text, duration);
        /*
         * String token="";//TrackmeToken.getToken(); String
         * userid="";//TrackmeToken.getUserId(); String
         * data="%7B"+"%22TokenKey%22:%22"
         * +token+"%22,%22Userlng%22:"+"%22"+lng+"%22,%22Userlat%22:%22"
         * +lat+"%22,%22UsrId%22:%22"+userid+"%22"+"%7D";//
         * "http://java.greymatterindia.com/montacts/web/trackme.php/api/updateLatLng?data="
         * //String
         * address=getString(R.string.sendLocationUrl)+data;//%7B"+"%22"+userid+"
         * %22:236,%22"+token+"%22:%22ADVWaQ==%22"+"%7D"; //
         * System.out.println("LatLong URL:-"+address); JSONObject json =
         * com.json.RestJsonClient .connect(data.trim());
         * System.out.println("LatLong"+json);
         */
   
   
   
    }
   
    @Override
    public void onDestroy() {
   
        System.out.println("destroy=========");
       
        locMan.removeUpdates(gpsListener);
       
    }

   
   
    public void onStart(Intent intent, int startId)
    {/*
       
       
        System.out.println("===============");
   
       
         gpsListener = new LocationListener()
        {
            public void onLocationChanged(Location location)
            {
                // Log.w("GPS", "Started");
                if (curLocation == null)
                {
                    curLocation = location;
                    locationChanged = true;
                }

                if (curLocation.getLatitude() == location.getLatitude()
                        && curLocation.getLongitude() == location.getLongitude())
                    locationChanged = false;
                else
                    locationChanged = true;

                curLocation = location;

                Toast.makeText(
                        getBaseContext(),
                        "Lat : " + String.valueOf(curLocation.getLatitude())
                                + "\n Long : "
                                + String.valueOf(curLocation.getLongitude()),
                        Toast.LENGTH_LONG).show();
               
                updateLocation(location.getLatitude(),location.getLongitude());
               
               
               
                 * if (locationChanged) locMan.removeUpdates(gpsListener);
               

            }

            public void onProviderDisabled(String provider) {

            }

            public void onProviderEnabled(String provider) {
                // Log.w("GPS", "Location changed", null);
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {
                if (status == 0)// UnAvailable
                {

                } else if (status == 1)// Trying to Connect
                {

                } else if (status == 2) {// Available

                }
            }

        };
       
        System.out.println("LocationUpdateService started......");

        timer.scheduleAtFixedRate(new TimerTask()
        {
            public void run()
            {
                double lat = curLocation.getLatitude();
                double lng = curLocation.getLongitude();
                Log.i(getClass().getSimpleName(), "Timer started!!!"+lat+" "+lng );

                System.out.print("Timer working");
                Log.i(getClass().getSimpleName(), "Timer started!!!");
                System.out.println(" Lattitude .."+lat+" longitude.."+lng);
               
                // _getWeatherUpdate();
            }
        }, 0, UPDATE_INTERVAL);
        Log.i(getClass().getSimpleName(), "Timer started!!!");

    */}

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

    @Override
    public void onCreate()
    {
        super.onCreate();

        System.out.println("LocationUpdateService created......");
        locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER))
        {
            locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4,
                    1, gpsListener);
        }
        else
        {
            this
                    .startActivity(new Intent(
                            "android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS"));
        }

        if (curLocation != null)
        {
            double lat = curLocation.getLatitude();
            double lng = curLocation.getLongitude();
            System.out.println("Lat : " + String.valueOf(lat) + "\n Long : "
                    + String.valueOf(lng));
           
        /*   
            Toast.makeText(
                    getBaseContext(),
                    "Lat : " + String.valueOf(lat) + "\n Long : "
                            + String.valueOf(lng), Toast.LENGTH_LONG).show();
*/
        }
        else{
       
           
           
            System.out.println("not able to track======");
        }
       
        // TODO Auto-generated method stub
       
    }

    /*@Override
    public boolean stopService(Intent name) {
        // TODO Auto-generated method stub
        System.out.println("LocationUpdateService stopped......");
        locMan.removeUpdates(gpsListener);
        return super.stopService(name);
    }*/

}

No comments:

Post a Comment