0

In GPS manager, I am getting the location update for every minutes in my Log.

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,60000,0, this);

This line did the trick.

LOG:

02-03 11:32:33.045    6217-6217/com.example.mylocation E/onLocationChanged﹕ 17.3732643
02-03 11:32:33.045    6217-6217/com.example.mylocation E/onLocationChanged﹕ 78.5046052
02-03 11:33:33.245    6217-6217/com.example.mylocation E/onLocationChanged﹕ 17.3732721
02-03 11:33:33.245    6217-6217/com.example.mylocation E/onLocationChanged﹕ 78.5046024
02-03 11:34:33.315    6217-6217/com.example.mylocation E/onLocationChanged﹕ 17.3732627
02-03 11:34:33.315    6217-6217/com.example.mylocation E/onLocationChanged﹕ 78.5046052
02-03 11:35:33.215    6217-6217/com.example.mylocation E/onLocationChanged﹕ 17.3732669
02-03 11:35:33.215    6217-6217/com.example.mylocation E/onLocationChanged﹕ 78.5046055
02-03 11:36:33.145    6217-6217/com.example.mylocation E/onLocationChanged﹕ 17.3732575
02-03 11:36:33.145    6217-6217/com.example.mylocation E/onLocationChanged﹕ 78.504606

How to update this information in my database.

This service starts on a button click, so I've implemented Asynctask in which http call send data to database

This is happening only once, data is entered in database only once i.e. on button click, whenever onLocationChanged() is called updated data not sent to database

public void onLocationChanged(Location location)
{
    latitude = location.getLatitude();
    longitude = location.getLongitude();

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);

    Log.e("onLocationChanged",Double.toString(latitude));
    Log.e("onLocationChanged", Double.toString(longitude));

}

Kindly help in solving this issue

kevz
  • 2,727
  • 14
  • 39
Prabs
  • 4,923
  • 6
  • 38
  • 59
  • Prathibha: i think you will need to call method for inserting data in `onLocationChanged` method which you are calling on Button click – ρяσѕρєя K Feb 03 '15 at 06:25
  • ρяσѕρєя K...these both are in different activity...onLocationChanged is in GPSTracker class and Asyncktask which sends the data to server is in mainactivity class – Prabs Feb 03 '15 at 06:32
  • Prathibha : then also separate logic for sending data to server in a separate class which you can use in both class's – ρяσѕρєя K Feb 03 '15 at 06:35
  • And i read many post related to how to call AsynckTask from another activity..they are really very very complicated – Prabs Feb 03 '15 at 06:39
  • ok...u mean create separate class which will just send data to server and invoke that class where ever needed – Prabs Feb 03 '15 at 06:41
  • yes yes . and my suggestion is use IntentService for sending data to server instead of AsynckTask. because using `IntentService` first benefit is easy to use from any class just class startService method by sending data in intent. second benefit is if user close your app during sending data to server then `IntentService` automatically stop after completing task – ρяσѕρєя K Feb 03 '15 at 06:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70107/discussion-between-prathibha-kirthi-and--k). – Prabs Feb 03 '15 at 06:46

2 Answers2

1

Your service starts on button click and location update is received in every one minute as you mentioned in your code,but the asynctask also starts on the button click and send the data to server,it completes it work and ends,it does not start again,so for sending data of every update to your server implement the asynctask in you onLocationChanged(Location location) method like this

public void onLocationChanged(Location location)
{
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0, this);

    Log.e("onLocationChanged",Double.toString(latitude));
    Log.e("onLocationChanged", Double.toString(longitude));
    new yourAsynctask(parameters).execute();

}
Pramod Yadav
  • 2,316
  • 2
  • 23
  • 36
  • i read some thing like over ride Asynctask..is it related to thst – Prabs Feb 03 '15 at 06:31
  • you don't need to override the asyntask you need to extend the aysntask class and override its methods – Pramod Yadav Feb 03 '15 at 06:33
  • sorry sorry...I mean use abstract class for Asynctask and then extend it – Prabs Feb 03 '15 at 06:34
  • you will have many examples for it on internet just try to search and extend asynctask and do your work – Pramod Yadav Feb 03 '15 at 06:35
  • if you have a look at this question..i've mentioned my code her http://stackoverflow.com/questions/28262338/what-should-be-the-onlocationchanged-content – Prabs Feb 03 '15 at 06:35
  • you have to call asynctask in onLocationChanged() as i told for every location update – Pramod Yadav Feb 03 '15 at 06:37
  • ok..so the line you have added "new yourAsynctask(parameters).execute(); " you mean to use the same ASynctask class which is used on button click or shall i declare another ASynctask class within GPStracker class with the same functioning – Prabs Feb 03 '15 at 06:38
  • in Gpstracker activity use ASynctask class which is declared in mainactivity with the same parameter...this is what u mean pramod?? – Prabs Feb 03 '15 at 06:44
  • the aysnctask in which you are sending request to server – Pramod Yadav Feb 03 '15 at 06:45
  • http://chat.stackoverflow.com/rooms/70107/discussion-between-prathibha-kirthi-and--k pramod once enter this discussion – Prabs Feb 03 '15 at 06:49
  • will you suggest how to achieve this task with a piece of code – Prabs Feb 03 '15 at 07:00
0

@kevz it was not possible without your help...Thank you soooo much..

Here the answer goes:

Step 1::move AsyncTask to GpsTracker class

step 2::start the service in MainActivity

Intent intent = new Intent(MainActivity.this, GPSTracker.class);
startService(intent);

step 3:: Use onStartCommand in GPSTracker.java

  @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        getLocation();
        return START_STICKY;
    }

It was not as simple as it look here..finally...COMPLETED..

Prabs
  • 4,923
  • 6
  • 38
  • 59