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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:46:36+00:00 2026-06-13T13:46:36+00:00

I am using a Service to get updates from the location, it’s NOT an

  • 0

I am using a Service to get updates from the location, it’s NOT an IntentService, but the log says that Activity has leaked IntentReceiver that was originally registered here. Are you missing a call to unregisterReceiver()?
I don’t use a Receiver, so I don’t register or unregister. Then, What’s the matter with this?

I paste my code for the Service:

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class UpdateService2 extends Service {

    private LocationManager locManager;
    private LocationListener locListener;
    private Location loc;
    public static int UPDATE_TIME   = 30000;
    public static long MAX_TIME     = 600000;
    public static long waited = 0;
    boolean active = true;
    String TAG = "UpdateService2";
    Thread myThread;
    SharedPreferences prefs;
    SharedPreferences.Editor editor;


    @Override
    public IBinder onBind(Intent arg0) {
        Log.d(TAG, "onBind");
        return null;
    }

    public void onCreate() {
        Log.d(TAG, "onCreate");
        SharedPreferences prefs = getSharedPreferences(MyConstants.MY_PREFERENCES,Context.MODE_PRIVATE);
        editor = prefs.edit();
        startGettingLocation();

        Log.d("UpdateService","Thread - active:"+active+", maxTime: "+MAX_TIME);
        myThread = new Thread() {
            public void run(){
                Log.d("UpdateService","run");
                try {
                    waited = 0;
                    Log.d("UpdateService","Thread - active:"+active+", maxTime: "+MAX_TIME+", waited: "+waited);
                    while(active && (waited < MAX_TIME)) {
                        sleep(10000);
                        if(active) {
                            waited += 10000;
                            Log.d("UpdateService","Thread update: "+waited/1000+" seg");
                        }
                    }
                } catch(InterruptedException e) {
                    Log.d("UpdateService","Exception: "+e.toString());
                } finally {
                    interrupt();
                }
            }
        };
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        active = false;
    }

    @Override
    public void onStart(Intent intent, int startid) {
        myThread.start();
        Log.d(TAG, "onStart");
    }

    private void startGettingLocation(){
        try {
            locListener = new LocationListener() {
                public void onLocationChanged(Location location) {
                    updatePosition(location);
                    Log.d("UpdateService","Update location - Lat:"+location.getLatitude()+", Lon:"+location.getLongitude());
                }
                public void onProviderDisabled(String provider){
                }
                public void onProviderEnabled(String provider){
                }
                public void onStatusChanged(String provider, int status, Bundle extras){
                }
            };
            locManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, UPDATE_TIME, 0, locListener);
            loc = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (loc != null) {
                updatePosition(loc);
            }
        } catch (Exception e){
            Log.d("UpdateService", e.toString());
        }
    }

    private void updatePosition(Location loc) {
        if(loc != null) {
            Double dLat = loc.getLatitude();
            Double dLon = loc.getLatitude();
            editor.putInt(MyConstants.PREFERENCES_LAT, dLat.intValue());
            editor.putInt(MyConstants.PREFERENCES_LON, dLon.intValue());
            editor.commit();
        }
    }

}

Then, the call from the activity onCreate is like this:

msgIntent = new Intent(this, UpdateService2.class);
startService(msgIntent);

And the call from the onDestroy is like this:

stopService(msgIntent);
  • 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-13T13:46:38+00:00Added an answer on June 13, 2026 at 1:46 pm

    This is “my” solution (it really comes from here: Alarms):

    On manifest, I register my broadcast receiver:

    <!-- Register BroadcastReceiver -->  
    <receiver android:name="com.example.MyReceiver"/>
    

    This is the implementation of the receiver:

    public class MyReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {  
            MyLocation myLocation = new MyLocation(context);
            myLocation.getLocation();
            SharedPreferences prefs = context.getSharedPreferences(MyConstants.MY_PREFERENCES,Context.MODE_PRIVATE);
            User.myLat =  (double) prefs.getInt(MyConstants.PREFERENCES_LAT, 0);
            User.myLon =  (double) prefs.getInt(MyConstants.PREFERENCES_LON, 0);
            User.isValidPosition = prefs.getBoolean(MyConstants.PREFERENCES_VALID, false);
    
            //TEST
            Toast.makeText(context, "Testing alarm onReceive - Lat:"+User.myLat/1E6
            +", Long:"+User.myLon/1E6+", isValid:"+User.isValidPosition, Toast.LENGTH_LONG).show();  
            Log.d("MyReceiver","Lat:"+User.myLat+", Long:"+User.myLon+", isValid:"+User.isValidPosition);
            }
    
        }
    

    The constants that I use (in another class):

    public class MyConstants {
        public static String MY_PREFERENCES = "MyPreferences";
        public static String PREFERENCES_LAT = "lat";   
        public static String PREFERENCES_LON = "lon";   
        public static String PREFERENCES_VALID = "isValid";
        public static final int ALARM_REQUEST_CODE = 1;
    }
    

    The class MyLocation, wich is the one that I call from the activity:

    public class MyLocation {
    private LocationManager locManager;
    private LocationListener locListener;
    private Location loc;
    private SharedPreferences prefs;
    private SharedPreferences.Editor editor;
    private Context context;
    private String TAG = "MyLocation";
    private int UPDATE_TIME = 0;
    
    public MyLocation(Context context){
        this.context = context;
        prefs = this.context.getSharedPreferences(MyConstants.MY_PREFERENCES,Context.MODE_PRIVATE);
        editor = prefs.edit();
    }
    
    public void getLocation(){
        //Register to get updates of the location
        locListener = new LocationListener() {
        public void onLocationChanged(Location location) {
        updatePosition(location);
        Log.d(TAG,"Update location - Lat:"+location.getLatitude()+", Lon:"+location.getLongitude());                                
    }
    
    public void onProviderDisabled(String provider){
    
    }
    public void onProviderEnabled(String provider){
    
    }
    public void onStatusChanged(String provider, int status, Bundle extras){
    
    }
    };
        startGettingLocation();
        locManager.removeUpdates(locListener);
    }
    
    private void startGettingLocation(){
        try {
            locManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
            locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, UPDATE_TIME, 0, locListener);
            loc = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (loc != null) {
            updatePosition(loc);
        }
        } catch (Exception e){
            Log.d(TAG, e.toString());       
        }
    }
    
    private void updatePosition(Location loc) {
    if(loc != null) {
        Double dLat = loc.getLatitude()*1E6;
        Double dLon = loc.getLongitude()*1E6;
        editor.putInt(MyConstants.PREFERENCES_LAT, dLat.intValue());
        editor.putInt(MyConstants.PREFERENCES_LON, dLon.intValue());
        editor.putBoolean(MyConstants.PREFERENCES_VALID, true);
        editor.commit();
    } 
    }
    
    }
    

    I put the references of latitude and longitude in the preferences to have easy access from the app to this data.

    Finally, this is the call from the activity to start getting my location:

    MyLocation myLocation;
    myLocation = new MyLocation(this);
    myLocation.getLocation();
    scheduleUpdates(30);
    

    In this case, I just set one alarm 30 seconds after the instant that I do, but this behavior is easy to program alarms to repeat any number of times.
    And this is the method that I use to stablish an alarm that wakes up the system even when the app is closed:

    private void scheduleUpdates(int when){  
    Log.d("CreateGroupFormActivity", "Alarm");
    AlarmManager manager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);  
    
    Intent intent  = new Intent(this, MyReceiver.class);  
    PendingIntent pIntent = PendingIntent.getBroadcast(this, MyConstants.ALARM_REQUEST_CODE, intent,  PendingIntent.FLAG_CANCEL_CURRENT);  
    manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + when * 1000, pIntent);       
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using JQGrid to get the date from the RESTFul web services that
Is there a library out there that will get you your approximate location using
I develop a web application that is getting user updates from a web service
I am using .NET remoting to retrieve periodic status updates from a Windows service
I often get to the question when using a service, should I find a
I get a CommunicationException while using WCF service. The message is: The remote endpoint
I plan on using JSONP to call an external web service to get around
Is there any way to get how much memory a service application is using
I am using service class which gives a call to my inner class that
Hi i am reading inbox using service but i m getting null pointer exception

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.