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 8583569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:34:09+00:00 2026-06-11T21:34:09+00:00

i am building a simple android application which require me to auto update the

  • 0

i am building a simple android application which require me to auto update the location. the auto update location will not only update when resuming the application but must auto update whenever the application is on. this application looks like the same with GPS system that were put inside the car. it get the exact location and always update the current location. i want to the same thing in here. i am building this application on android 2.2.

here is my code:

 package com.example.map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;
import android.widget.Toast;

public class MapActivity extends Activity implements LocationListener{
    private TextView latituteField, longitudeField, accuracyField, altitudeField, bearingField; 
    private LocationManager locationManager;
    private String provider;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    latituteField = (TextView) findViewById(R.id.TextView02);
    longitudeField = (TextView) findViewById(R.id.TextView04);
    accuracyField = (TextView) findViewById(R.id.textView1);
    altitudeField = (TextView) findViewById(R.id.textView2);
    bearingField = (TextView) findViewById(R.id.textView4);

    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use default  
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);


    // Initialize the location fields
    if (location != null) {
      System.out.println("Provider " + provider + " has been selected.");
      onLocationChanged(location);
    } else {
      latituteField.setText("Location not available");
      longitudeField.setText("Location not available");
    }
  }

  /* Request updates at startup */
  @Override
  protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider, 400, 1, this);
  }

  /* Remove the locationlistener updates when Activity is paused */
  @Override
  protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
  }

  @Override
  public void onLocationChanged(Location location) {
    int lat = (int) (location.getLatitude());
    int lng = (int) (location.getLongitude());
    int acc = (int) (location.getAccuracy());
    int alt = (int) (location.getAltitude());
    int bearing = (int) (location.getBearing());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));
    accuracyField.setText(String.valueOf(acc));
    altitudeField.setText(String.valueOf(alt));
    bearingField.setText(String.valueOf(bearing));

  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

  }

  @Override
  public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
        Toast.LENGTH_SHORT).show();

  }

  @Override
  public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Disabled provider " + provider,
        Toast.LENGTH_SHORT).show();
  }

this code it only update the location value when im resuming the application (i quit the application then go back to the application). the location will be updated at the viewtext.

i also tried to do while. like this.

if(location != null){
    do{
        System.out.println("Provider " + provider + " has been selected.");
        onLocationChanged(location);
        locationManager.requestLocationUpdates(provider, 0, 0, this);
    }while(location != null);
    }
    else{
        latituteField.setText("Location not available");
        longitudeField.setText("Location not available");
    }

im adding this to the manifest.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" /> 

however, this code makes the application crash in a real phone. is there anyway to get this done?

*note: i tested all this application in my real phone and trying to achieve the current location. the textview suppose to change whenever it gets the new value of the location without pausing or quitting the application.

  • 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-06-11T21:34:10+00:00Added an answer on June 11, 2026 at 9:34 pm

    You have to put your location code inside an ScheduledExecutor Service:

    http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html

    That one repeats actualization function in the background for example every 30 seconds or whatever time you specify.

    Example:

        scheduleTaskExecutor= Executors.newScheduledThreadPool(5);
    
        // This schedule a task to run every 10 seconds:
    
        scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
          public void run() {
    
    
                try {                 
    
    
                    //add your code you would like to be executed every 10 seconds.
    
    
    
                } catch (IOException e) {                          
                   e.printStackTrace();                           
                }                                     
    
    
          }
        }, 0, 10, TimeUnit.SECONDS);
    

    you also might have to add a permission for coarse location.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Android development. I'm building a simple Android application which analyzes accelerometer
I am building a small Android application which will add a cube on user's
I have a requirement for building an Android mapping application onto which I will
I'm building a fairly simple Android app (sdk revision 14: ICS) which allows users
Im building a simple chat client which is only supposed to be able to
I am building a simple java/android application, and am trying to change the value
I am currently building a simple application for Android using the NDK and needs
I'm building simple application for myself in JSP which stores URL for me and
I'm building an application which relies on the Google Maps API, but running into
I'm building a simple contact form for a website. It does not connect 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.