onLocationChanged never gets called, even after requesting for updates - android

Start Service:
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, LocationService.class);
pi = PendingIntent.getService(this, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pi);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), 30*1000, pi);
Service:
public class LocationService extends Service implements LocationListener {
public static double curLat = 0.0;
public static double curLng = 0.0;
private LocationManager mgr;
private String best;
private Location location;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
best = LocationManager.NETWORK_PROVIDER;
location = mgr.getLastKnownLocation(best);
if (location != null) {
dumpLocation(location);
mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
5 * 1000, 0, this);
}
return START_NOT_STICKY;
}
}
private void dumpLocation(Location l) {
SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy:hh:mm:ss",
Locale.ENGLISH);
String format = s.format(l.getTime());
//The above time is always 21/03/2013:09:53:41 which is more than a week old
curLat = l.getLatitude();
curLng = l.getLongitude();
}
#Override
public void onLocationChanged(Location location) {
//This never seems to be called
dumpLocation(location);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
Every 30 secs the service is being called, but the requestLocationUpdates never seems to call onLocationChanged. And when I checked the time of the lastKnownLocation its a week old.
Applications like foursquare, local, maps etc gives the current location with GPS disabled.
What am I missing?

I think you need to move from somewhere with your device because onLocationChange only called if the location was change. And the network location was approximate near by 1000-2000 meter so need to check on street where the location was change and getting the new location. If you used gps then it will giving you accurate but it also nearby 20-30 meter after fixed all it.

Related

How to find my current location (latitude + longitude) in every 5 second in Android?

I tried many code snippets but all the example takes too much time to provide latitude and longitude .
I want to get current latitude in every 5 seconds .
You can use this post to achieve what you are looking for. Look at the setInterval(5000) will do that
you can check and integrate this library which is a wrapper for google play services location which also handles permissions.
google location
In this lib you can find various methods to set param
You can use FusedLocationProviderAPI to make your application location aware.
Using the Google Play services location APIs, your app can request the last known location of the user's device. In most cases, you are interested in the user's current location, which is usually equivalent to the last known location of the device.
For receiving most accurate location updates you can use this solution.
https://developer.android.com/training/location/index.html
You can create service that broadcasts your latitude and longitude in every 5 seconds using LocationManager.
public class MyLocationService extends Service {
private LocationManager locationManager;
private LocationListener locationListener;
public final String APP_BROADCAST_RECEIVER = "LatLonReciever"; // You should define it somewhere else as a static. e.g within Const class
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (locationListener == null ) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
locationIsChanged(location);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
};
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 5000, locationListener); // here 5000 is 5 seconds
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void locationIsChanged(Location location){
Intent intent = new Intent(APP_BROADCAST_RECEIVER);
intent.putExtra("lat", location.getLatitude());
intent.putExtra("lon", location.getLongitude());
sendBroadcast(intent);
}
}
Also, don't foret to register the reciever where you want to recieve the results:
public final String APP_BROADCAST_RECEIVER = "LatLonReciever";
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// Here you do what you want with recieved intent that contains lat and lon
}
};
IntentFilter intentFilter = new IntentFilter(APP_BROADCAST_RECEIVER);
registerReceiver(broadcastReceiver, intentFilter);

Cannot able to get Location exactly every 10 minutes using Pending Intent and AlarmManager

I posted my Requirement,Problem and code,I don't know what is wrong with my code,Any help..
My Requirement : I want to get latitude,longtitude from GPS every 10 minutes
Problem : I am Keep on Getting latitude,longtitude
My Code :
1].I am using this code in activity,I am starting Broadcast Receiver from activity
//Pending Intent
Intent in = new Intent(this,GPSReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, in, 0);
//Alaram manager
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
am.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), 10*60*1000, pi);
2].BroadCast Class "GPSReceiver.class",From here I am starting service names as GPSService
public class GPSReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context,GPSService.class);
context.startService(service);
}
}
3].Service Class "GPSService",This is for getting location updates
public class GPSService extends Service {
#Override
public IBinder onBind(Intent arg0) {
return null;
}
//Flag to know GPS Status
boolean isGPSEnabled = false;
LocationManager locationmanager;
String locationgpsprovider = LocationManager.GPS_PROVIDER;
double latitude;
double longtitude;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//To get Location service
locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
//To get GPS Status
isGPSEnabled = locationmanager.isProviderEnabled(locationgpsprovider);
if (!isGPSEnabled) {
Toast.makeText(this, "Please check your GPS connection", Toast.LENGTH_LONG).show();
}else {
locationmanager.requestLocationUpdates(locationgpsprovider, 0, 0, ll);
}
return START_NOT_STICKY;
}
LocationListener ll = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(Location location) {
Log.i("Hei", "10 minutes up");
latitude = location.getLatitude();
longtitude = location.getLongitude();
}
};
Everythime you alarm goes on it delivers another Intent to Service and locationmanager starts all over again. You don't need an Alarm here since LocationManager already deliveries location at specified intervals.
It's enough starting your Service only once and instantiate your locationmanager stuff at onCreate. In this case use onStartCommand only to return START_NOT_STICKY.
Tip 1: Define a condition to stop the service, don't let it run forever.
Tip 2:Take a look at requestLocationUpdates javadoc to setup a proper minTime.
Tip 3:You can find more tips in Android docs:
http://developer.android.com/training/location/index.html

Gps tracking in android

I'm trying to write application that track my gps coordinates.
Every 10 seconds i want to send my coordinates to server - for this i using AlarmManager.
For getting coordinates i'm using Service that implement onClickListener.
How i start service:
public void startAlarm() {
Intent intent = new Intent(this, GpsService.class);
PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, 0,
10 * 1000, pintent);
}
My gps service:
public class GpsService extends Service implements LocationListener {
// Declaring a Location Manager
private LocationManager locationManager;
Location location; // location
double latitude; // latitude
double longitude; // longitude
double accuracy;
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 10 * 1; //10secs
#Override
public void onCreate() {
super.onCreate();
if(locationManager == null ) {
locationManager = (LocationManager) this
.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
getLocation();
if (location != null) {
//send to server
}
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
locationManager.removeUpdates(this);
locationManager = null;
super.onDestroy();
}
#Override
public void onLocationChanged(Location location) {
Log.v("myLogs", "GetLocation service: ONLOCATIONCHANGED");
this.location = location;
}
public Location getLocation() {
try {
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isGPSEnabled ) {
stopSelf();
} else {
this.canGetLocation = true;
if (isGPSEnabled) {
if (location == null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
}
It work perfect on emulator, but on real device it only show GPS Finding icon. What is problem?

Getting Speed from Android GPS

I want to get the speed out of the LocationManager in my app. I have a Criteria with setSpeedRequired(true) attribute. I am doing location.getSPeed(), but it gives me 0 all the time. Below is the code for the GPS which I am running as a Service.
public class Tracking extends Service implements LocationListener {
protected LocationManager locationManager;
Location location;
double latitude;
double longitude;
float velocity;
String provider;
private static final long minDist = 0;
private static final long minTime = 0;
LocationDatabaseHandler ldb;
#Override
public void onCreate() {
ldb = new LocationDatabaseHandler(this);
getLocation();
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent,flags,startId);
//because we do not want to stop the service unless we explicitly say so.
return START_STICKY;
}
public Location getLocation() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setSpeedRequired(true);
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDist,
this);
location = locationManager.getLastKnownLocation(provider);
if(location != null) {
onLocationChanged(location);
}
return location;
}
#Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
batteryP = getBatteryPerc();
velocity = location.getSpeed();
insertIntoDb(latitude, longitude, velocity, "onchanged");
}
public void insertIntoDb(double latitude, double longitude, float velocity, String where) {
Date date = new Date();
String dateStr = date.toString();
ldb.addLocation(new Locations(latitude, longitude, dateStr, velocity));
}
}
When I see my database, the velocity is always 0.0. Is there something I am missing?
locationManager.requestLocationUpdates(provider, minTime, minDist, this);

Android locationManager - "location not changed" event

I'm building a service that will let me know if the location hasn't change in a period of time for a certain amount of metres.
Then thing is I have the event onLocationChanged on my Listener.. but I don't know how to do the opposite.. that is, send a broadcast if the location is within the distance I provided after some minutes.
This is the code I have so far
LocationService
public class LocationService extends Service {
public static final String LOC_INTENT = "com.xxx.intent.action.LOCATION";
private Thread triggerService;
protected LocationManager locationManager;
protected MyLocationListener MyLocationListener;
protected Criteria criteria;
public static final int MIN_TIME = 300000; // 5 Minutes
public static final long MIN_DISTANCE_MOTOR = 50; // 50 Metres
private SharedPreferences settings;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
settings = getSharedPreferences(getString(R.string.settings_prefsName), 0);
addLocationListener();
return START_STICKY;
}
private void addLocationListener()
{
triggerService = new Thread(new Runnable(){
public void run(){
try{
Looper.prepare();//Initialise the current thread as a looper.
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
final String PROVIDER = locationManager.getBestProvider(criteria, true);
updateLocation(getLastBestLocation(MIN_TIME, MIN_DISTANCE_MOTOR));
MyLocationListener = new MyLocationListener();
locationManager.requestLocationUpdates(PROVIDER, MIN_TIME, MIN_DISTANCE_MOTOR, MyLocationListener);
Log.d("LOC_SERVICE", "Service RUNNING! ("+PROVIDER+")");
Looper.loop();
}catch(Exception ex){
ex.printStackTrace();
}
}
}, "LocationThread");
triggerService.start();
}
public Location getLastBestLocation(int minDistance, long minTime) {
Location bestResult = null;
float bestAccuracy = Float.MAX_VALUE;
long bestTime = Long.MIN_VALUE;
List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
float accuracy = location.getAccuracy();
long time = location.getTime();
if ((time > minTime && accuracy < bestAccuracy)) {
bestResult = location;
bestAccuracy = accuracy;
bestTime = time;
}
else if (time < minTime && bestAccuracy == Float.MAX_VALUE && time > bestTime) {
bestResult = location;
bestTime = time;
}
}
}
return bestResult;
}
public static void updateLocation(Location location)
{
Context appCtx = MyApplication.getAppContext();
double latitude, longitude;
float speed;
latitude = location.getLatitude();
longitude = location.getLongitude();
speed = location.getSpeed();
Intent filterRes = new Intent();
filterRes.setAction(LOC_INTENT);
filterRes.putExtra("latitude", latitude);
filterRes.putExtra("longitude", longitude);
filterRes.putExtra("speed", speed);
appCtx.sendBroadcast(filterRes);
}
class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location location)
{
if(settings.getBoolean("active", false))
updateLocation(location);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
Set a timer for however long you want to test this. When it goes off, check if the last location you got in onLocationChanged is older than the timer length.
EDIT
Here is how I would imagine your service looking
Service starting
requestLocationUpdates called with appropriate minimum time and minimum distance that that you will be notified after
Repeating task set where you check if an update was received (check out Timer.scheduleAtFixedRate
Service running
Perform necessary actions when your timer goes off or onLocationChanged is called
Service stopping
Remove location updates with removeUpdates
Stop your timer

Categories

Resources