shashank kapoor
shashank kapoor

Reputation: 45

Android service not stopping

I have created a service that sends the location of the device to a server where then that location is stored in a DB.

In that service I have implemented a LocationListner and inside the onLocationChanged method im taking the locations sending it to the server through HttpPost setting the alarm for the service (As I want it to send the location every hour) and then stopping the service.

The problem is even when ive called 'stopself' the service continues to run for a few times before stopping (As in sends the location a few times more). How can i change it so that he service stops instantly.

PFB the code for the onLocationChanged method

public void onLocationChanged(android.location.Location location) {
            if(location!=null)
            {
                System.out.println("here");
                latid = location.getLatitude();
                longid = location.getLongitude();
                 String userinfo = ``Double.toString(latid)+"&&&&"+Double.toString(longid);
                 Location = userinfo;

                 Toast.makeText(getApplicationContext(), Location, 1000).show();

                 HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://www.easymontra.in/location/try.php");

                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                    nameValuePairs.add(new BasicNameValuePair("lat", Double.toString(latid)));
                    nameValuePairs.add(new BasicNameValuePair("long", Double.toString(longid)));
                    try {
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    // Execute HTTP Post Request
                    try {
                        HttpResponse response = httpclient.execute(httppost);
                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);



                    Calendar calendar = Calendar.getInstance();

                    calendar.setTimeInMillis(System.currentTimeMillis());

                    calendar.add(Calendar.MINUTE, 60);

                    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);

                    Toast.makeText(getApplicationContext(), "stopping now", 1000).show();
                   stopSelf();



            }

Upvotes: 0

Views: 804

Answers (1)

Akhil
Akhil

Reputation: 14038

stopSelf() is a request and it depends on the runtime as to when it processes the request. It is an asynchronous method call, so one may not expect the service to end immediately when this method gets called. Also, it seems in your case the few more location updates are getting triggered by the time your service is stopped, so you could instead stop listening for location updates using locationManager.removeUpdates(locationListener); putting it before stopSelf();

Upvotes: 1

Related Questions