0

i have a class(location2.java) that finds location for me,I use this code in my class : What is the simplest and most robust way to get the user's current location on Android?

and I have a service that override that abstract "locationResult";Now i want my service after running its codes,service doesn't finish and stay alive for receiving location from location2.java.

appreciating any help for this.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    Location2.LocationResult locate = new Location2.LocationResult() {
        @Override
        public void gotLocation(Location location1, Boolean Gps, Boolean Net) {

            if (location1 != null) {
                Log.e("Loc", String.valueOf(location1.getLatitude()));

            }

            try {
                //this is a method that i want to be run after receiving location from location2.java
                json_maker(location1, speed_computation(location1), Gps, Net);


            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    };
    Location2 location = new Location2();
    location.getLocation(context, locate);


    return Service.START_FLAG_REDELIVERY;

}
Community
  • 1
  • 1
Samaneh Rezaei
  • 73
  • 2
  • 11
  • 1
    The most successful way is to use return START_STICKY. – W4R10CK Sep 05 '16 at 10:37
  • 1
    @W4R10CK thank u,but as I know "return" in services is used for a time that service was terminated by system ,and if service wants to restart, multiple constants for example START_STICKY can be used.doesn't it? – Samaneh Rezaei Sep 05 '16 at 11:39

1 Answers1

0

The most successful way is to use return START_STICKY.

"and if service wants to restart, multiple constants for example START_STICKY can be used.doesn't it?" - Yes, we can use.

START_STICKY Constant to return from onStartCommand(Intent, int, int) if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Developer guide: Android

W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • thanks , I want my service stay alive, i do not have any idea about time that my service was killed.what can i do my service doesn't finish after doing its methods, and wait for another class? – Samaneh Rezaei Sep 05 '16 at 11:56
  • @sama There are two Service IntentService and Service. IntentService stopped by the user after work has done. Service keeps working in the background and in Main UI Thread. Use a Thread to run your service. Select and upvote for the correct answer :). – W4R10CK Sep 05 '16 at 11:59
  • I'll be so thankful if u say a bit in details,how does thread help me? – Samaneh Rezaei Sep 05 '16 at 13:28
  • @sama http://www.vogella.com/tutorials/AndroidServices/article.html use this link for Service documents and uses. – W4R10CK Sep 05 '16 at 13:32
  • @ W4R10CK thank u,but i couldn't do any thing for my code. – Samaneh Rezaei Sep 08 '16 at 14:31