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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:05:24+00:00 2026-06-13T02:05:24+00:00

I’m trying to send text messages containing the users location every so often using

  • 0

I’m trying to send text messages containing the users location every so often using the timer. Initially, I ran into a nullpointerexception which was due to a simple mistake I made. Once this was fixed everything seemed to be running fine. However, it never gets my location, so, the text that keeps sending is saying “Could Not receive location”.

What I am asking is why is it not getting my location? And what can I do to fix the problem?

There are no logcat errors and I am using two emulators to test this app (send text from one to the other). If you could provide any help or resolution it would be greatly appreciated! And if I’m overlooking something that may seem to be very simple please alert me that I am doing so. Thanks!

Here’s the code:

    public class MessageService extends Service{
int counter = 0;
private Timer timer = new Timer();
public String textTime, phoneNumber;
public int updateInterval;
int lat, lng;
String coordinates, latitude, longitude;
LocationManager locationManager;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    //receives the intent extras from the calling intent
    textTime = intent.getStringExtra("textTime");
    phoneNumber = intent.getStringExtra("phone");

    phoneNumber = "5556";

    //the following if statement has to do with transferring the string textTime into a number that can be used
    if (textTime.equals("15 Minutes")) {
        updateInterval = (15 * (60000));
    }else if (textTime.equals("30 Minutes")) {
        updateInterval = (30 * (60000));
    }else if (textTime.equals("1 Hour")){
        updateInterval = (60 * (60000));
    }else {
        updateInterval = (15 * (60000));
    }

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    new Thread(){
        public void run(){
            Looper.prepare();
            // Define a listener that responds to location updates
            LocationListener locationListener = new LocationListener() {
                public void onLocationChanged(Location location) {
                  // Called when a new location is found by the network location provider.
                  //makeUseOfNewLocation(location);
                    lat = (int) (location.getLatitude() * 1E6);
                    lng = (int) (location.getLongitude() * 1E6);

                    latitude = Integer.toString(lat);
                    longitude = Integer.toString(lng);

                    coordinates = "Coordinates: " + latitude + ", " + longitude + ". Latitude: " + latitude + " Longitude: " + longitude + ". Respond 'END' to stop texts."; 
                }

                public void onStatusChanged(String provider, int status, Bundle extras) {}

                public void onProviderEnabled(String provider) {}

                public void onProviderDisabled(String provider) {}
              };

              Looper.loop();

              if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
              }else{
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
              }

        }
    }.start();


    //the following method should use a timer to send a sms message in a timed interval. It also should implement using a different thread
    doSomethingRepeatedly();

    return START_STICKY;

}

public void doSomethingRepeatedly(){
    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            //the following code should be what is done repeatedly
            //Log.d("MessageService", String.valueOf(++counter));

            sendSmsMessage();
        }
    }, 0, updateInterval);
}

//the following code handles sending the text message
public void sendSmsMessage(){
    SmsManager sms = SmsManager.getDefault();
    if (coordinates == null || coordinates.equals("")){
        coordinates = "Could Not Receive Location";
    }

    sms.sendTextMessage(phoneNumber, null, coordinates , null, null);

}

public void onDestroy() {
    super.onDestroy();

    if (timer != null) {
        timer.cancel();
    }
}




}//end of service
  • 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-13T02:05:27+00:00Added an answer on June 13, 2026 at 2:05 am

    Your LocationListener is probably being destroyed before onLocationChanged() can be called. Your Service should implement the LocationListener itself.


    public class MessageService extends Service implements LocationListener {
        int counter = 0;
        private Timer timer = new Timer();
        public String textTime, phoneNumber;
        public int updateInterval;
        int lat, lng;
        String coordinates, latitude, longitude;
        LocationManager locationManager;
    
        @Override
        public IBinder onBind(Intent arg0) {
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId){
            //receives the intent extras from the calling intent
            textTime = intent.getStringExtra("textTime");
            phoneNumber = intent.getStringExtra("phone");
    
            phoneNumber = "5556";
    
            //the following if statement has to do with transferring the string textTime into a number that can be used
            if (textTime.equals("15 Minutes")) {
                updateInterval = (15 * (60000));
            }else if (textTime.equals("30 Minutes")) {
                updateInterval = (30 * (60000));
            }else if (textTime.equals("1 Hour")){
                updateInterval = (60 * (60000));
            }else {
                updateInterval = (15 * (60000));
            }
    
            locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    
            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            }else{
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
            }
    
            //the following method should use a timer to send a sms message in a timed interval. It also should implement using a different thread
            doSomethingRepeatedly();
    
            return START_STICKY;
    
        }
    
        public void doSomethingRepeatedly(){
            timer.scheduleAtFixedRate(new TimerTask() {
                public void run() {
                    //the following code should be what is done repeatedly
                    //Log.d("MessageService", String.valueOf(++counter));
    
                    sendSmsMessage();
                }
            }, 0, updateInterval);
        }
    
        //the following code handles sending the text message
        public void sendSmsMessage(){
            SmsManager sms = SmsManager.getDefault();
            if (coordinates == null || coordinates.equals("")){
                coordinates = "Could Not Receive Location";
            }
    
            sms.sendTextMessage(phoneNumber, null, coordinates , null, null);
    
        }
    
        public void onDestroy() {
            super.onDestroy();
    
            if (timer != null) {
                timer.cancel();
            }
        }
    
        @Override
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location provider.
            //makeUseOfNewLocation(location);
            lat = (int) (location.getLatitude() * 1E6);
            lng = (int) (location.getLongitude() * 1E6);
    
            latitude = Integer.toString(lat);
            longitude = Integer.toString(lng);
    
            coordinates = "Coordinates: " + latitude + ", " + longitude + ". Latitude: " + latitude + " Longitude: " + longitude + ". Respond 'END' to stop texts."; 
        }
    
        @Override
        public void onProviderDisabled(String provider) {}
    
        @Override
        public void onProviderEnabled(String provider) {}
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    }
    
    • 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 ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of 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.