I have changed my application over to a new project, new API key and a new package name. For some reason this has fixed my problem! I am getting a fix almost immediately after my application starts! Very strange.
Maps API V2 Location update taking very long
-
06-07-2023 - |
Pregunta
I'm having a weird problem that I can't seem to figure out. If I open the Maps app on the phone I get a fix almost straight away but my app on the other hand either sometimes takes very long to get a fix or on some occasions even refuses to outright do it! I am using FragmentActivity btw. Here is my code:
onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMapIfNeeded();
//This keeps the screen On while app is running
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
LocationManager lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setSpeedRequired(false);
// Getting the name of the best provider
String bestProvider = lm.getBestProvider(criteria, true);
// Getting Current Location with GPS
//loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);
//lm.requestLocationUpdates(lm.GPS_PROVIDER, 1000, 1, this);
loc = lm.getLastKnownLocation(bestProvider);
lm.requestLocationUpdates(bestProvider, 1000, 1, this);
Log.d(TAG, "Location " + loc);
broadcastReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
Object tmp = intent.getParcelableExtra(ActivityRecognitionService.RECOGNITION_RESULT);
addUpdate((ActivityRecognitionResult)tmp);
}
};
}
As you can see I tried specifying the GPS as the provider but it doesn't really change a thing since I'm using a phone that only has GPS and Wifi (basically a tablet). Ignore the broadcastReceiver thing there that's for Activity Recognition like Still, Walking, In_Vehicle etc.
setupMapIfNeeded and setupMap
private void setUpMapIfNeeded() {
// TODO Auto-generated method stub
// Do a null check to confirm that we have not already instantiated the map.
if (map == null)
{
// Try to obtain the map from the SupportMapFragment.
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (map != null)
{
setUpMap();
}
//This is how you register the LocationSource
map.setLocationSource(this);
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera.
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap()
{
map.setMyLocationEnabled(true);
map.setOnMapLongClickListener(this);
}
This is all straight forward.
onLocationChangedListener
Override
public void activate(OnLocationChangedListener locationChangedListener) {
// TODO Auto-generated method stub
locListener = locationChangedListener;
}
@Override
public void deactivate() {
// TODO Auto-generated method stub
locListener = null;
}
@Override
public void onLocationChanged(Location loc) {
//Push location updates to the registered listener
//This ensures my-location layer retrieves the new/received location
if (locListener != null) {
locListener.onLocationChanged(loc);
Utils.showInfoMessage(this, "Got a location");
Log.d(TAG, "onLocationChanged");
if(loc.hasSpeed()){
float speed = loc.getSpeed();
Log.d(TAG, "Speed = " + speed);
};
//Animate camera to center of phone location
map.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(loc.getLatitude(), loc.getLongitude())));
// Toast.makeText(getApplicationContext(), "Location Changed",
// Toast.LENGTH_SHORT).show();
}
}
So yeah, I have no idea why it does that but I have a feeling it's got something to do with me getting this recurring errors in the All Messages filter of Logcat. One of them says:
Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@42a36178 that was originally bound here
And the other says this:
java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL
Can someone please help me? It's getting very frustrating as one day the app seems to work the next it doesn't and it's sending me up the walls! It's my Uni dissertation as well!
Solución