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

  • Home
  • SEARCH
  • 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 6345379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:48:35+00:00 2026-05-24T20:48:35+00:00

I’m supposed to update the listener every 5 seconds. But it doesn’t. What could

  • 0

I’m supposed to update the listener every 5 seconds. But it doesn’t. What could be wrong with my code?

Thanks a lot for any help!

package com.example.android.lbs;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class LbsGeocodingActivity extends Activity {

    public static final String LOG_TAG = "<<< ------------ >>> GeocodingActivity <<< ------------ >>>";

    TextView tv;
    int ScreenOutputCount;
    String newTrip;

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 5000; // in Milliseconds, 5 secconds

    protected LocationManager lm;
    MyLocationListener locationL = new MyLocationListener();

    protected Button retrieveLocationButton;
    //Log.e(LOG_TAG, e.toString());

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);

        try {
            View.OnClickListener handler = new View.OnClickListener(){
                public void onClick(View v) {
                    //we will use switch statement and just
                    //get thebutton's id to make things easier
                    switch (v.getId()) {

                        case R.id.trip_start: 
                            tripStart();
                            break;
                        case R.id.trip_end: 
                            tripEnd();
                        break;
                    }
                }
            };

            //we will set the listeners
            findViewById(R.id.trip_start).setOnClickListener(handler);
            findViewById(R.id.trip_end).setOnClickListener(handler);

            }catch(Exception e){
                 Log.e("Android Button Tutorial", e.toString());
            }

    }    

    public void tripStart(){

        File originalDirectories = new File( Environment.getExternalStorageDirectory() + "/Android/GpsTracker/" );
        if( !originalDirectories.exists() ){
            originalDirectories.mkdirs();
        }

        LbsGeocodingActivity.this.newTrip = getNewFileName();
        LbsGeocodingActivity.this.ScreenOutputCount = 1;
        LbsGeocodingActivity.this.tv = (TextView) this.findViewById( R.id.thetext );
        LbsGeocodingActivity.this.tv.setText("Location Updates: \n");

        LbsGeocodingActivity.this.lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        LbsGeocodingActivity.this.lm.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 
                MINIMUM_TIME_BETWEEN_UPDATES, 
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                this.locationL
        );

        Toast.makeText(getBaseContext(), "New Trip Started.", Toast.LENGTH_SHORT).show();

    }

    public String getNewFileName(){
        SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdfDateTime.format(new Date(System.currentTimeMillis()));    
    }

    public void tripEnd(){
        LbsGeocodingActivity.this.lm.removeUpdates( this.locationL );
        Toast.makeText(getBaseContext(), "Trip Ended.", Toast.LENGTH_SHORT).show();
    }

    protected void showCurrentLocation() {
        try{
            Location location = LbsGeocodingActivity.this.lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

            if (location != null) {
                String show_curent = "Longitude: " + location.getLongitude()
                                        + " | Latitude: " + location.getLatitude();

                //LbsGeocodingActivity.this.tv.append( show_curent );

                Toast.makeText(getBaseContext(), show_curent, Toast.LENGTH_SHORT).show();

                //LbsGeocodingActivity.this.ScreenOutputCount = LbsGeocodingActivity.this.ScreenOutputCount + 1;
            }else{
                Toast.makeText(getBaseContext(), "Last Know Location Not Found.", Toast.LENGTH_SHORT).show();
            }
        }catch( NullPointerException e){
            Log.e("Android Button Tutorial", e.toString() );
        }
    }   

    private class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location location) {
            LbsGeocodingActivity.this.tv.append( LbsGeocodingActivity.this.ScreenOutputCount
                    + " | Longitude: " + location.getLongitude()
                    + " | Latitude: " + location.getLatitude() + "\n" );

            LbsGeocodingActivity.this.ScreenOutputCount = LbsGeocodingActivity.this.ScreenOutputCount + 1;

            logLocationUpdate( location.getLongitude(), location.getLatitude() );
        }

        public void logLocationUpdate( double longitude, double latitude){
            try {

                SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String timestamp =  sdfDateTime.format(new Date(System.currentTimeMillis()));

                String logString = timestamp + "::" +
                                    longitude + "::" +
                                    latitude + "\n";

                String sdcard = Environment.getExternalStorageDirectory() + "/Android/GpsTracker/";

                //then write the file
                final String TESTSTRING = new String(logString);

                File gpxfile = new File(sdcard, LbsGeocodingActivity.this.newTrip + ".txt" );
                FileWriter gpxwriter = new FileWriter( gpxfile, true );
                BufferedWriter out = new BufferedWriter( gpxwriter );
                out.write(TESTSTRING);
                out.close();

            }catch(Exception e){
              Log.e("GPS Tracker", e.getMessage());
            }
        }
        public void onStatusChanged(String s, int i, Bundle b) {
            /*
            Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
                    Toast.LENGTH_LONG).show();
            */
        }

        public void onProviderDisabled(String s) {
            Toast.makeText(LbsGeocodingActivity.this,
                    "Provider disabled by the user. GPS turned off",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) {
            Toast.makeText(LbsGeocodingActivity.this,
                    "Provider enabled by the user. GPS turned on",
                    Toast.LENGTH_LONG).show();
        }

    }

}
  • 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-24T20:48:36+00:00Added an answer on May 24, 2026 at 8:48 pm

    By setting it to 5000 ms it doesn’t necessarily mean that you will get the location every 5th second. The time is more or less a best case scenario.

    minTime the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
    http://developer.android.com/reference/android/location/LocationManager.html

    Are you sure you have your activity running? If you want to recieve the location when the activity is sleeping you need a wake lock and a service.

    For example in a GPS app I created I put 0 ms to get it as soon as possible but when debugging and checking the logcat I sometimes get the location every minute or so, not every second.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but
I want to construct a data frame in an Rcpp function, but when I

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.