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);
This is “my” solution (it really comes from here: Alarms):
On manifest, I register my broadcast receiver:
This is the implementation of the receiver:
The constants that I use (in another class):
The class MyLocation, wich is the one that I call from the activity:
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:
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: