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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:33:40+00:00 2026-06-17T14:33:40+00:00

I am trying to create a background service that updates the current gps position.

  • 0

I am trying to create a background service that updates the current gps position. I am getting a NullPointerException at line LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

HomeActivity launches a service

startService(new Intent(getApplicationContext(), ForHire.class));

Service (ForHire) creates a TimerTask Updates

public class ForHire extends Service {

...
private Timer getUpdatesNow = new Timer();
private Updates updates = new Updates(getUpdatesNow);

@Override
public void onCreate() {
    ...
    getUpdatesNow.schedule(updates, God.KM20TIME);
    Log.v("Taxeeta", "Notification created");
}

    private class Updates extends TimerTask implements LocationListener {
    private Timer getUpdatesNow;

    public Updates(Timer newGetUpdatesNow) {
        super();
        getUpdatesNow = newGetUpdatesNow;
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                God.KM20TIME, God.KM20DISTANCE, (LocationListener) this);
    }
    public void run() {
        ...
        //do some cleanup
    }
            @Override
    public void onLocationChanged(Location location) {
        Log.v("Taxeeta", "Location changed");
        // do a update of the current location.
    }

The first issue is that I get that NullPointerException.
Second issue is that onLocationChanged is never called if I comment out LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, God.KM20TIME, God.KM20DISTANCE, (LocationListener) this); these 2 lines.

My Manifest

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application ...>
   ...
   <uses-library android:name="com.google.android.maps" />
</application>

What am I missing here ?

Edit : Values of KM20TIME = 5000 (5seconds), and KM20DISTANCE = 1 (1meter). Apart from the fix’s below, I walked out of my house, got GPS enabled and walked from one corner of my balcony to the other. I noticed that my gps (LSB) changed as I walked from one corner to the other every 5 seconds.

  • 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-17T14:33:41+00:00Added an answer on June 17, 2026 at 2:33 pm

    The earliest you can call new Updates(getUpdatesNow); is inside your Service’s onCreate() method. The simple reason is that getSystemService() requires a valid Context which doesn’t exist before entering onCreate().

    Try:

    private Timer getUpdatesNow;
    private Updates updates;
    
    @Override
    public void onCreate() {
        ...
        getUpdatesNow = new Timer();
        updates = new Updates(getUpdatesNow);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a background service that every X minutes consult a webservice
Trying to create a background-image slideshow and am getting this error... This is the
What i'm trying to create is a background worker that executes a few processes
I want to create a background service which continuously check that is there any
I am trying to do the following: Create background service When a message is
I`m trying to create a file downloader as a background service but when a
I am trying to create a service that will handle file I/O in the
I am trying to create a background service in asp.net which checks for session
Background : I am trying to create an SMS API service. The developers have
Possible Duplicate: requestLocationUpdates throws exception I'm trying to create a service that when the

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.