How to stop tracking location?











up vote
0
down vote

favorite












I have a location-based alerting app when a user enters near to particular area, he/she should be alerted. I need to stop tracking location after the alert is clicked once. But my app is continuously alerting the user. How to stop it?
I included code to check the distance between the user current location and destination in location onChange method.



public void onLocationChanged(Location location) {
double lat1 = location.getLatitude();
double lon1 = location.getLongitude();
checkloc(lat1, lon1);
}









share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a location-based alerting app when a user enters near to particular area, he/she should be alerted. I need to stop tracking location after the alert is clicked once. But my app is continuously alerting the user. How to stop it?
    I included code to check the distance between the user current location and destination in location onChange method.



    public void onLocationChanged(Location location) {
    double lat1 = location.getLatitude();
    double lon1 = location.getLongitude();
    checkloc(lat1, lon1);
    }









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a location-based alerting app when a user enters near to particular area, he/she should be alerted. I need to stop tracking location after the alert is clicked once. But my app is continuously alerting the user. How to stop it?
      I included code to check the distance between the user current location and destination in location onChange method.



      public void onLocationChanged(Location location) {
      double lat1 = location.getLatitude();
      double lon1 = location.getLongitude();
      checkloc(lat1, lon1);
      }









      share|improve this question















      I have a location-based alerting app when a user enters near to particular area, he/she should be alerted. I need to stop tracking location after the alert is clicked once. But my app is continuously alerting the user. How to stop it?
      I included code to check the distance between the user current location and destination in location onChange method.



      public void onLocationChanged(Location location) {
      double lat1 = location.getLatitude();
      double lon1 = location.getLongitude();
      checkloc(lat1, lon1);
      }






      android android-studio location locationmanager






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 18:49









      Federico Grandi

      2,53021026




      2,53021026










      asked Nov 19 at 15:42









      Sireesha Devineni

      1




      1
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote













          You might want to have a Flag, representing if the user was already warned, for instance:



          boolean WAS_WAARNED = false;
          void onLocationChanged(Location location) {
          if(!WAS_WARNED){
          WAS_WARNED = true;
          checkloc(location.getLatitude(), location.getLongitude());
          }
          }





          share|improve this answer




























            up vote
            0
            down vote













            First Approach->
            Save alert clicked the flag to PrefUtils which is the most useable memory for save status. When a user clicks the alert you just set the status true and save it to prefUtils.



            public void onLocationChanged(Location location) {
            if(PrefUtils.getLoationStatus(getApplicationContext(), false)) {
            double lat1=location.getLatitude();
            double lon1=location.getLongitude();
            checkloc(lat1,lon1);
            PrefUtils.setLocationStatus(true);
            }
            }


            Second Approach ->
            When the alert is clicked just set -> locationManager.removeUpdates(this);






            share|improve this answer




























              up vote
              0
              down vote













              You'll want to stop requesting location updates. This can be done by calling removeLocationUpdates on your location client.



              For example:



              alertButton.setOnClickListener(view -> {
              //Do some stuff.

              if (mLocationClient != null){
              mLocationClient.removeLocationUpdates(mLocationCallback)}
              });


              Here, mLocationClient is your FusedLocationProviderClient, and mLocationCallback is your LocationCallback (this will have the onLocationChanged method within it). Since you check their location in the onLocationChanged method, there will no longer be updates to the location so the alert won't be created. You could also call removeLocationUpdates in your checkloc method if they are in the correct location.



              Here is a link to the documentation for the FusedLocationProviderClient, giving more information.



              Note: you may want to look up geofencing. It does something similar to what you want to do.






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378128%2fhow-to-stop-tracking-location%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                0
                down vote













                You might want to have a Flag, representing if the user was already warned, for instance:



                boolean WAS_WAARNED = false;
                void onLocationChanged(Location location) {
                if(!WAS_WARNED){
                WAS_WARNED = true;
                checkloc(location.getLatitude(), location.getLongitude());
                }
                }





                share|improve this answer

























                  up vote
                  0
                  down vote













                  You might want to have a Flag, representing if the user was already warned, for instance:



                  boolean WAS_WAARNED = false;
                  void onLocationChanged(Location location) {
                  if(!WAS_WARNED){
                  WAS_WARNED = true;
                  checkloc(location.getLatitude(), location.getLongitude());
                  }
                  }





                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    You might want to have a Flag, representing if the user was already warned, for instance:



                    boolean WAS_WAARNED = false;
                    void onLocationChanged(Location location) {
                    if(!WAS_WARNED){
                    WAS_WARNED = true;
                    checkloc(location.getLatitude(), location.getLongitude());
                    }
                    }





                    share|improve this answer












                    You might want to have a Flag, representing if the user was already warned, for instance:



                    boolean WAS_WAARNED = false;
                    void onLocationChanged(Location location) {
                    if(!WAS_WARNED){
                    WAS_WARNED = true;
                    checkloc(location.getLatitude(), location.getLongitude());
                    }
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 19 at 15:54









                    Bonatti

                    2,37341533




                    2,37341533
























                        up vote
                        0
                        down vote













                        First Approach->
                        Save alert clicked the flag to PrefUtils which is the most useable memory for save status. When a user clicks the alert you just set the status true and save it to prefUtils.



                        public void onLocationChanged(Location location) {
                        if(PrefUtils.getLoationStatus(getApplicationContext(), false)) {
                        double lat1=location.getLatitude();
                        double lon1=location.getLongitude();
                        checkloc(lat1,lon1);
                        PrefUtils.setLocationStatus(true);
                        }
                        }


                        Second Approach ->
                        When the alert is clicked just set -> locationManager.removeUpdates(this);






                        share|improve this answer

























                          up vote
                          0
                          down vote













                          First Approach->
                          Save alert clicked the flag to PrefUtils which is the most useable memory for save status. When a user clicks the alert you just set the status true and save it to prefUtils.



                          public void onLocationChanged(Location location) {
                          if(PrefUtils.getLoationStatus(getApplicationContext(), false)) {
                          double lat1=location.getLatitude();
                          double lon1=location.getLongitude();
                          checkloc(lat1,lon1);
                          PrefUtils.setLocationStatus(true);
                          }
                          }


                          Second Approach ->
                          When the alert is clicked just set -> locationManager.removeUpdates(this);






                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            First Approach->
                            Save alert clicked the flag to PrefUtils which is the most useable memory for save status. When a user clicks the alert you just set the status true and save it to prefUtils.



                            public void onLocationChanged(Location location) {
                            if(PrefUtils.getLoationStatus(getApplicationContext(), false)) {
                            double lat1=location.getLatitude();
                            double lon1=location.getLongitude();
                            checkloc(lat1,lon1);
                            PrefUtils.setLocationStatus(true);
                            }
                            }


                            Second Approach ->
                            When the alert is clicked just set -> locationManager.removeUpdates(this);






                            share|improve this answer












                            First Approach->
                            Save alert clicked the flag to PrefUtils which is the most useable memory for save status. When a user clicks the alert you just set the status true and save it to prefUtils.



                            public void onLocationChanged(Location location) {
                            if(PrefUtils.getLoationStatus(getApplicationContext(), false)) {
                            double lat1=location.getLatitude();
                            double lon1=location.getLongitude();
                            checkloc(lat1,lon1);
                            PrefUtils.setLocationStatus(true);
                            }
                            }


                            Second Approach ->
                            When the alert is clicked just set -> locationManager.removeUpdates(this);







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 19 at 16:09









                            Mahabub Karim

                            212




                            212






















                                up vote
                                0
                                down vote













                                You'll want to stop requesting location updates. This can be done by calling removeLocationUpdates on your location client.



                                For example:



                                alertButton.setOnClickListener(view -> {
                                //Do some stuff.

                                if (mLocationClient != null){
                                mLocationClient.removeLocationUpdates(mLocationCallback)}
                                });


                                Here, mLocationClient is your FusedLocationProviderClient, and mLocationCallback is your LocationCallback (this will have the onLocationChanged method within it). Since you check their location in the onLocationChanged method, there will no longer be updates to the location so the alert won't be created. You could also call removeLocationUpdates in your checkloc method if they are in the correct location.



                                Here is a link to the documentation for the FusedLocationProviderClient, giving more information.



                                Note: you may want to look up geofencing. It does something similar to what you want to do.






                                share|improve this answer



























                                  up vote
                                  0
                                  down vote













                                  You'll want to stop requesting location updates. This can be done by calling removeLocationUpdates on your location client.



                                  For example:



                                  alertButton.setOnClickListener(view -> {
                                  //Do some stuff.

                                  if (mLocationClient != null){
                                  mLocationClient.removeLocationUpdates(mLocationCallback)}
                                  });


                                  Here, mLocationClient is your FusedLocationProviderClient, and mLocationCallback is your LocationCallback (this will have the onLocationChanged method within it). Since you check their location in the onLocationChanged method, there will no longer be updates to the location so the alert won't be created. You could also call removeLocationUpdates in your checkloc method if they are in the correct location.



                                  Here is a link to the documentation for the FusedLocationProviderClient, giving more information.



                                  Note: you may want to look up geofencing. It does something similar to what you want to do.






                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    You'll want to stop requesting location updates. This can be done by calling removeLocationUpdates on your location client.



                                    For example:



                                    alertButton.setOnClickListener(view -> {
                                    //Do some stuff.

                                    if (mLocationClient != null){
                                    mLocationClient.removeLocationUpdates(mLocationCallback)}
                                    });


                                    Here, mLocationClient is your FusedLocationProviderClient, and mLocationCallback is your LocationCallback (this will have the onLocationChanged method within it). Since you check their location in the onLocationChanged method, there will no longer be updates to the location so the alert won't be created. You could also call removeLocationUpdates in your checkloc method if they are in the correct location.



                                    Here is a link to the documentation for the FusedLocationProviderClient, giving more information.



                                    Note: you may want to look up geofencing. It does something similar to what you want to do.






                                    share|improve this answer














                                    You'll want to stop requesting location updates. This can be done by calling removeLocationUpdates on your location client.



                                    For example:



                                    alertButton.setOnClickListener(view -> {
                                    //Do some stuff.

                                    if (mLocationClient != null){
                                    mLocationClient.removeLocationUpdates(mLocationCallback)}
                                    });


                                    Here, mLocationClient is your FusedLocationProviderClient, and mLocationCallback is your LocationCallback (this will have the onLocationChanged method within it). Since you check their location in the onLocationChanged method, there will no longer be updates to the location so the alert won't be created. You could also call removeLocationUpdates in your checkloc method if they are in the correct location.



                                    Here is a link to the documentation for the FusedLocationProviderClient, giving more information.



                                    Note: you may want to look up geofencing. It does something similar to what you want to do.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 19 at 16:10

























                                    answered Nov 19 at 16:01









                                    Kay Tukendorf

                                    65




                                    65






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378128%2fhow-to-stop-tracking-location%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                                        Alcedinidae

                                        Can an atomic nucleus contain both particles and antiparticles? [duplicate]