Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6623693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:33:46+00:00 2026-05-25T21:33:46+00:00

I am writing a location service App that log where the user has been

  • 0

I am writing a location service App that log where the user has been every minute.
Should I create a service for the GPS process? OR just create the LocationManager at the Activity? Which one is better?

Moreover, I have tried to hide the application by pressing hardware home button and turn off GPS at Setting -> Location. I found that the App closed automatically within an hour.
Is it possible to keep the application always alive?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:33:46+00:00Added an answer on May 25, 2026 at 9:33 pm

    I highly recommend creating the gps at the very least as a thread in the activity, if you want to be slick set it up as a service and broadcast intents from inside an asynctask. Setting it up as a service makes it a bit modular if you want to use it for other applications or in other activities. Thats the way I implemented it.

    Its also easier to control the lifetime of your gps readings if you run it from a service instead of your activity, so service doesnt get interrupted if you do switch activities etc.. example of asynctask portion below:

        /** Begin async task section ----------------------------------------------------------------------------------------------------*/
        private class PollTask extends AsyncTask<Void, Void, Void> { //AsyncTask that listens for locationupdates then broadcasts via "LOCATION_UPDATE" 
            // Classwide variables
            private boolean trueVal = true;
            Location locationVal;
            //Setup locationListener
            LocationListener locationListener = new LocationListener(){ //overridden abstract class LocationListener
                @Override
                public void onLocationChanged(Location location) {
                    handleLocationUpdate(location);
                }
                @Override
                public void onProviderDisabled(String provider) {
                }
                @Override
                public void onProviderEnabled(String provider) {
                }
                @Override
                public void onStatusChanged(String provider, int status,
                        Bundle extras) {
                }
            };
    
            /** Overriden methods */
            @Override 
            protected Void doInBackground(Void... params) { 
                //This is where the magic happens, load your stuff into here
                while(!isCancelled()){ // trueVal Thread will run until you tell it to stop by changing trueVal to 0 by calling method cancelVal(); Will also remove locationListeners from locationManager
                    Log.i("service","made it to do in background");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    }
                return null; 
    
            }
    
            @Override
            protected void onCancelled(){
                super.onCancelled();
                stopSelf();
            }
    
            @Override
            protected void onPreExecute(){ // Performed prior to execution, setup location manager
                locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                if(gpsProvider==true){
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                }
                if(networkProvider==true){
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
                }
            }
    
            @Override 
            protected void onPostExecute(Void result) { //Performed after execution, stopSelf() kills the thread
                stopSelf(); 
            } 
    
            @Override
            protected void onProgressUpdate(Void... v){ //called when publishProgress() is invoked within asynctask
                    //On main ui thread, perform desired updates, potentially broadcast the service use notificationmanager
                    /** NEED TO BROADCAST INTENT VIA sendBroadCast(intent); */
                    Intent intent = new Intent(LOCATION_UPDATE);
                    //Put extras here if desired
                    intent.putExtra(ACCURACY, locationVal.getAccuracy()); // float double double long int
                    intent.putExtra(LATITUDE, locationVal.getLatitude());
                    intent.putExtra(LONGITUDE, locationVal.getLongitude());
                    intent.putExtra(TIMESTAMP, locationVal.getTime());
                    intent.putExtra(ALTITUDE,locationVal.getAltitude());
                    intent.putExtra(NUM_SATELLITES,0);/////////////****TEMP
                    sendBroadcast(intent); //broadcasting update. need to create a broadcast receiver and subscribe to LOCATION_UPDATE
                    Log.i("service","made it through onprogress update");
            }
    
            /** Custom methods */
    
            private void cancelVal(){ //Called from activity by stopService(intent) --(which calls in service)--> onDestroy() --(which calls in asynctask)--> cancelVal()
                trueVal = false;
                locationManager.removeUpdates(locationListener);
            }
    
            private void handleLocationUpdate(Location location){ // Called by locationListener override.
                locationVal = location;
                publishProgress();
            }
    
        } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The app that I'm writing uses the GPS location manager service requestLocationUpdates() but I
I'm writing an app that monitors the user's location. I have a CLLocationManager object
I am writing an android app using the GPS location provider in a Service
I'm writing an iPhone App that relies on getting the device location. Management have
I've been tasked with writing a web service that can be called from one
I am writing an app that will use Reverse Geocoding to translate the user's
I am writing small geolocation service: then user come to my site I should
I'm writing an android app that takes your current location and returns a list
I'm writing my first location based android app, but got confused about some of
I'm trying to create a plugin for nopCommerce v2.20 that allows the user to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.