I have tried both GPS and Network Provider but they give null location for getLastKnownLocation. I do not get any onLocationChanged either. I am trying from my apartment where the cell signal is strong. I can even browse internet well and download and install apps fine from market.
I have read few threads on same topic but their suggestion seems I already have tried.
Here is my code snippet.
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if(null == locationManager) {
Log.d(TAG, "location manager NULL");
}
geocoder = new Geocoder(this);
// Initialize with the last known location
Location lastLocation = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (lastLocation != null)
onLocationChanged(lastLocation);
else
Log.i(TAG, "null LastKnownLocation");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
super.onStartCommand(intent, flags, startId);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0,0, this);
Log.d(TAG, "requested location updates");
return START_STICKY;
}
As per log:
mitenm#pinkydebian:/media/sf_E_DRIVE/tmp$ adb logcat LocationNotifierService:* *:S
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
I/LocationNotifierService(30866): null LastKnownLocation
D/LocationNotifierService(30866): gps provider enabled:true
D/LocationNotifierService(30866): network provider enabled:true
D/LocationNotifierService(30866): onCreate
D/LocationNotifierService(30866): requested location updates
D/LocationNotifierService(30866): requested location updates
D/LocationNotifierService(30866): requested location updates
I have "unlock receiver" so when I unlock my screen I request location updates which is working as expected from logs. But the onLocationChanged method is not getting invoked.
I have permission added to manifest both for fine and coarse.
Regards,
Miten.
android getLaskKnownLocation null
As Per Google Doc
getLaskKnownLocation Returns a Location indicating the data from the last known location fix obtained from the given provider otherwise it returns null.
It means it shows the cached data.If you are running your application on device which has never queried for location data before will have empty cache thats why you are getting null.
do not get any onLocationChanged either.
Make sure you have enabled Network Provider for location.
Use below Snippet.
if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected() || connectivityManager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI).isConnected()) {
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Constants.CONNECTED = true;
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 5000, 1, this);
} else {
Constants.CONNECTED = false;
Toast.makeText(
context, "Please enable Wireless networks in Location Setting!!", 10000).show();
}
}
Related
I'd like to check the user's assisted GPS setting in Android. When I change the 'location accuracy' setting, a ContentObserver tells me the URI is content://settings/global/assisted_gps_enabled. I don't see a constant for this in android.provider.Settings.Global; how do I access the value of this setting?
Try the following code to read that value:
int value = 0;
ContentResolver cr = getContentResolver();
try {
value = Settings.Global.getInt(cr,"assisted_gps_enabled");
} catch (Exception e) {
android.util.Log.e("get agps_enabled",e.toString());
}
results on Android 8.0, It returns 1 (Enabled by default)
To write that value:
Settings.Global.putInt(cr, "assisted_gps_enabled", your-value);
This permission <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/> is required and this permission is only granted to system apps
A-GPS is turned on in phones when a user has set GPS settings to "Battery savings" and in order to use it in your app, you will have to add permission:
android.permission.ACCESS_COARSE_LOCATION
in your manifest xml file, if you are running app below android 6.0 (Marshmellow), else you will have to ask permissions at runtime,
see here:
https://developer.android.com/training/permissions/requesting
Now, how you will got to know programatically that user has using AGPS or GPS you can use LocationManager class for that and can pass either LocationManager.NETWORK_PROVIDER; for AGPS or LocationManager.GPS_PROVIDER for GPS respectively
for GPS provider, in android settings it is termed as "High accuracy" setting and for that permission required is android.permission.ACCESS_FINE_LOCATION
how to use it?
see here: https://developer.android.com/guide/topics/location/strategies#java
for further help, please have a look:
for runtime permissions: https://developer.android.com/training/permissions/requesting
for AGPS
https://en.wikipedia.org/wiki/Assisted_GPS
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// 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);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
What is the best way to track the latitude / longitude of the device in Android. I've tried many methods, non of which are working at all.
My device is running Android 4.1.2.
In my current code, sometimes GPS will work perfectly fine but sometimes it won't work (giving me latitude 0 and longitude 0).
Here is my current code:
locationManager = mLocationManager;
locationListener = new LocationListener() {
/**
* Fired when the location from the sensor changes.
*
* #param location Location object.
*/
#Override
public void onLocationChanged(Location location) {
sLatitude = String.valueOf(location.getLatitude());
sLongitude = String.valueOf(location.getLongitude());
sLatLon = sLatitude.concat(",").concat(sLongitude);
Log.d("TOMTOM LOCATION", location.toString());
}
/**
* Fired when the provider's status changes.
*
* #param provider The provider that has changed status.
* #param status The new status of the provider.
* #param extras Any extra data.
*/
public void onStatusChanged(String provider, int status, Bundle extras) {
// Do nothing.
}
/**
* Fired when the provider has been enabled.
*
* #param provider Provider that has been enabled.
*/
#Override
public void onProviderEnabled(String provider) {
// Do nothing.
}
/**
* Fired when the provider has been disabled.
*
* #param provider Provider that has been disabled.
*/
#Override
public void onProviderDisabled(String provider) {
// Do nothing.
}
};
Log.d("TOMTOM", locationManager.getProviders(true).toString());
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d("TOMTOM", "GPS enabled");
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
Log.d("TOMTOM", "Request GPS updates");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
Log.d("TOMTOM", "Request network updates");
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
} else {
Log.d("TOMTOM", "Fail");
}
}
} else {
Log.d("TOMTOM", "GPS disabled");
Log.d("TOMTOM", "Request network updates");
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
} else {
Log.d("TOMTOM", "Fail");
}
}
Any ideas why sometimes I get GPS updates (Logged as Request GPS updates) and sometimes it will fail completely (Logged as Fail)?
It's been a year or so since I did GPS stuff on Android, so bear with me if I am not entirely right. Maybe someone can correct me.
The GPS system is not instant. You can not start your application and expect to have a location instantly. I believe the reason you get to the log telling you it's a Fail simply is because Android does not always have a last known location. So you can get in situations where GPS is neither available (In buildings for example) where you also don't have a meaningful last known location. In this case it will simply be null.
this is how I register my app to receive location updates:
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(Consts.ONE_MINUTE * 10);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setFastestInterval(Consts.ONE_MINUTE);
Builder builder = new GoogleApiClient.Builder(context);
builder.addApi(ActivityRecognition.API);
mGoogleApiClient = builder.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
....
....
#Override
public void onConnected(Bundle connectionHint) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, locationUpdatespendingInent);
}
my pending intent been invoked in background almost in the exact requested intervals...
so far so good.
the problem: When WIFI is disabled/ not connected to any network, or when there is no 3G/4G network data enabled - the fused location provider not providing new location updates!!
my Location access settings are turned on, and GPS satellites and WI-FI & mobile network location is checked.
the even bigger problem: sometimes in that case, I do receive location updates callbacks via the pending intent, but with the last location it knew (even if it was an hour ago, and I'm long long gone away miles from that place)
according to the documentation of PRIORITY_BALANCED_POWER_ACCURACY :
Used with setPriority(int) to request "block" level accuracy.
Block level accuracy is considered to be about 100 meter accuracy. Using a coarse accuracy such as this often consumes less power.
I'm expecting that the fused location provider will open the GPS when it have no other choice, or at least won't provide a new location updates if he don't have any.
another unpredictable and disturbing issue:
I changed PRIORITY_BALANCED_POWER_ACCURACY to PRIORITY_HIGH_ACCURACY in order to see how it behaves (for 24 hours). all the intervals stayed the same (10 minutes interval between updates). accurate location indeed received even in phones with no network/sim card, but - the battery drained out fast! when I looked on the battery history, I was surprised to see that GPS radio was on full transmission mode all the time!!!! and I saw also in my log that loction was received every minute, even that I requested location each ten minutes (I don't have any other installed apps that opens GPS to receive locations..)
I noticed this behavior on several devices (such as Moto X 2013, HTC One X, Nexus 5) , all with latest Google Play Services (version 6.1.11) , and android KITKAT 4.4.4
my application depends a lot on the user current location, and periodically receives location updates in the specified interval as long as the user logged in, so I don't want to use the PRIORITY_HIGH_ACCURACY mode, to prevent battery drain..
my questions:
is the fused location provider suppose to use GPS at all if it set to receive updates with PRIORITY_BALANCED_POWER_ACCURACY and don't have any WI-FI or cell towers info ?
if it does, then what am I doing wrong?
why I'm getting this misleading location updates that are not correct? (as I explained in the the "even bigger problem" section..
why GPS radio is opened all the time instead of been opened for the 10 minutes interval when I used the PRIORITY_HIGH_ACCURACY parameter? (I don't have other installed apps that triggers location updates faster..)
For the questions specified,
1. is the fused location provider suppose to use GPS at all if it set to receive updates with PRIORITY_BALANCED_POWER_ACCURACY and don't have any WI-FI or cell towers info ? &
2. if it does, then what am I doing wrong?
Apparently no explicitly unique source is specified anywhere within documentation. With either PRIORITY options, even through code, the "source" of obtained location is "fused".
[location.getProvider() returns :"fused"]
I have seen GPS being used only when the LocationRequest has PRIORITY_HIGH_ACCURACY. So it does not use GPS under other conditions.
4. why GPS radio is opened all the time instead of been opened for the 10 minutes interval when I used the PRIORITY_HIGH_ACCURACY parameter? (I don't have other installed apps that triggers location updates faster..)
The fastest interval has been set for 1 minute. From what i understood, the setFastestInterval is given precedence over setInterval when the value for fastest interval is shorter in duration than the value of setInterval.
In your case, 1 minute against 10.
About not having other installed apps that triggers location updates, its just given as an example and not specified that only that case explicitly.
This controls the fastest rate at which your application will receive
location updates, which might be faster than setInterval(long) in some
situations (for example, if other applications are triggering location
updates).
So, what happens is with PRIORITY_HIGH_ACCURACY, it requests location on the fastest interval set - 1min, by using GPS(kind of exclusively).
3. why I'm getting this misleading location updates that are not correct? (as I explained in the the "even bigger problem" section..
Need to check the code for pendingIntent mechanism also. Though there could be a few things to take note of:
You can add a location.getTime() to ensure and verify the time of obtained location. Very likely it is not being updated, if there is no wifi-cell towers in range and PRIORITY_BALANCED_POWER_ACCURACY is used.
A block level accuracy of location on the first place, which is being used when "lastKnown" is called wouldn't help.
The battery consumption was because of the combination of GPS and 1 min updates. Try setting the fastest interval as 5 or 10 mins, if that is suitable for your implementation but PRIORITY_BALANCED_POWER may not help if you need absolutely accurate location. I normally add a check for the location obtained in onLocationChanged and depending on that, switch the priority in LocationRequest. It helps in, surely, obtaining a location generally, unless i am inside a building with no line-of-sight for GPS and Wifi-Network are off.
I would suggest you to use AlarmManager and FusedLocationProvider together in such a way that your AlarmManager fire alarm on every 10minute with a view to start location updates.
Once you get updated location, disconnect the location client. You don't need to keep it running for all the time by setting interval time in LocationRequest, Instead you can invoke the process on each time interval by using AlarmManager.
In such kind of mechanism, you will have following benefits which will resolve your problems:
GPS radio will stay open only for few seconds while retrieving location because you are going to disconnect after getting first location update. Thus GPS radio will not stay open all the time so the battery will be saved.
You will be able to get new location on each 10minutes without messing around with old location or something.
I hope it will be helpful.
Cell towers cover a several mile area so they aren't the best for getting a location from. Look at the location accuracy when you are working with locations.
#Override
public void onLocationChanged(Location location) {
//it happens
if (location == null) {
return;
}
// all locations should have an accuracy
if (!location.hasAccuracy()) {
return;
}
// if its not accurate enough don't use it
// this value is in meters
if (location.getAccuracy() > 200) {
return;
}
}
You could put a broadcastreceiver on the network status and when there is no connection you could restart your location provider with priority_high_accuracy which will use the GPS only when the user has the GPS enabled otherwise it falls back on the wifi and cell towers.
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
/** Checks whether the device currently has a network connection */
private boolean isDeviceOnline() {
ConnectivityManager connMgr = (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
For updating of the GPS coordinates you can use GPS and WI-FI providers also. For updating of the position use as well minimum distance parameter. I will provide you with small GPS service example.
Answers :
1) PRIORITY_BALANCED_POWER_ACCURACY do not use GPS.
2) Use GPS and WI-FI to detect location.
3) PRIORITY_BALANCED_POWER_ACCURACY probably because of no WI-FI in area.
Code example :
public class GPSservice extends Service implements LocationListener {
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 2;
private static final long MIN_TIME_BW_UPDATES = 1000 * 1;
double latitude, longitude;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
Location location;
protected LocationManager locationManager;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
getLocation();
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onLocationChanged(Location location) {
new LocationReceiver(location, getApplicationContext());
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
public Location getLocation() {
try {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
Log.d("Network", "NO network");
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
}
Right, the situation is as follows;
We have developed an app that checks the location of the user every 5-10 minutes for location specific content. To do so, I've created a Service to stick to the background (so it can update even when the app isn't directly in the foreground) in which a LocationController is created to check the latest known location. When it's finished, it cleans up and the location is sent to a database (which is a necessity for this concept).
This all works fine, as long as I check the location with GPS_PROVIDER. However, when I switch it around to NETWORK_PROVIDER, it may check the location once more before dying completely.
I've tried multiple options to prevent this problem, but nothing seems to be working with the update service when I swap that 1 setting.
Here are a few snippets which should be relevant to the service:
UpdateService:
#Override
public void onCreate() {
super.onCreate();
Log.d("Debug", "Created service");
PowerManager mgr = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
IntentFilter filter = new IntentFilter();
filter.addAction(UPDATE_ACTION);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Starting", "Starting Service");
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(UPDATE_ACTION);
alarmManagerPendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);
alarmManagerPendingIntent.cancel();
mAlarmManager.cancel(alarmManagerPendingIntent);
mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
AlarmManager.INTERVAL_FIFTEEN_MINUTES,
AlarmManager.INTERVAL_FIFTEEN_MINUTES,
alarmManagerPendingIntent);
mLocationController = new LocationController(this, this);
updateHandler = new Handler();
return START_STICKY;
}
LocationController:
private LocationListener locationListener = new LocationListener() {
private boolean didSendLocation;
public void onLocationChanged(Location location) {
mLastLocation = location;
mCallback.locationUpdate(location,false);
didSendLocation = true;
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Debug", "Status changed: "+status);
Location _lastLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("Debug", "Last location: "+_lastLocation);
if(!didSendLocation)
{
mCallback.locationUpdate(_lastLocation,false);
}
didSendLocation = false;
}
public void onProviderEnabled(String provider) {
Log.d("Debug", "Provider Enabled");
}
public void onProviderDisabled(String provider) {
Log.d("Debug", "Provider disabled");
}
};
public LocationController(Context mContext, LocationControllerCallback callback) {
this.mContext = mContext;
mCallback = callback;
Log.d("Debug", "Created LocationController");
mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 100, locationListener);
mLastLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
mCallback.locationUpdate(mLastLocation,true);
}
So, the code works as it is now, but when I swap:
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 100, locationListener);
with
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 300000, 100, locationListener);
It will no longer update. Any ideas?
When you use this to get location updates:
mLocationManager.requestLocationUpdates(...)
you don't need AlarmManager methods as the first parameter of the requestLocationUpdates method already acts as a "timer". See here for more info.
Also, you may need to include the following directives in your AndroidManifest.xml file:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"/>
Without these permissions, the requestLocationUpdates method may not work as you expected regardless of which provider you use.
UPDATE 1:
I would have your LocationController class initialiased in a Service, for example:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
this.mLocationController = new LocationController(this);
return Service.START_NOT_STICKY;
}
Then, start the above Service in the onResume method of an activity (MainActivity or another activity in your project), and pause the Service in the onPause method of the same activity.
If its specific to the NETWORK_PROVIDER, you may be seeing this bug:
https://code.google.com/p/android/issues/detail?id=57707
Original bug report included the Samsung Galaxy S3 as an affected device.
To help troubleshoot - you may also want to try selecting the NETWORK_PROVIDER in the GPS Benchmark app (full disclosure - its my app) to see if this app exhibits the same behavior. If so, its likely an issue with the device.
If you determine you're seeing the above issue, please be sure to star it on the issue page if you'd like to see it fixed.
I've solved the problem by switching to the Google Play Fused Location provider. A more detailed post around this can be found here: https://stackoverflow.com/a/19282976/3532181
Sadly the standard network provider just seemed too unreliable when it came to updates, ranging from an update within 8 minutes to hours before a new update was sent to the app.
I had a similar issue. I was able to fix this by adding the required permissions. The issue is related to new background service limitations.
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
Check this documentation for better understanding.
I have a client-server application where the client sends a "ping" and the server responds back with a "pong". On receipt of the "pong", the client sends its location updates (GPS data) to the server. On receiving the location updates, the server sends a "pong" and this goes on for a while.
The sockets (for sending and receiving messages) are created by the client and server in separate threads. I register the LocationListener in the main thread. The problem is that, I do not get any updates from the GPS. I checked the GPS by running a separate app that displays the number of satellites seen and the time taken for the first fix. It took about 90 seconds for the first fix.
The problem I have is very similar to the ones mentioned here and here. Also here.
My code is given below:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.getBestProvider(criteria, true);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isGPSEnabled) {
Log.i(TAG,"PING: GPS not enabled");
} else {
Log.i(TAG,"PING: GPS enabled");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
Log.i(TAG,"PING: adding GPS status listener");
locationManager.addGpsStatusListener(PingActivity.this);
}
/*The server and client threads are started after this.*/
The LocationListener is as follows:
LocationListener locListener = new LocationListener()
{
#Override
public void onLocationChanged(Location location) {
Log.i(TAG, "PING: onLocationChanged");
Log.i(TAG, "PING: location is " + location.getLatitude() + ", " + location.getLongitude());
}
As you can see, there are just two log statements in the LocationListener and these two log statements are not printed at all. Is it because I have a thread constantly listening for updates and the LocationListener is never invoked? I also tried creating a separate activity for the GPS and registering that before starting the client-server threads.
These are the permissions in the manifest file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
The message I get in LogCat is
duplicate add listener for uid
Can someone throw some light on this? Thanks.
You getting error for the code (extract from android source)
private void handleAddListener(int uid) {
synchronized(mListeners) {
if (mClientUids.indexOfKey(uid) >= 0) {
// Shouldn't be here -- already have this uid.
Log.w(TAG, "Duplicate add listener for uid " + uid);
return;
}
mClientUids.put(uid, 0);
if (mNavigating) {
try {
mBatteryStats.noteStartGps(uid);
} catch (RemoteException e) {
Log.w(TAG, "RemoteException in addListener");
}
}
}
}
mClientUids is declared as
private final SparseIntArray mClientUids = new SparseIntArray();
And, in your code,
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
locationManager.addGpsStatusListener(PingActivity.this);
In here you are adding two different listener and that is what Android is complaining about.