I would like to locate the user by clicking on a button. Therefore I use this code:
lm = getSystemService(LocationManager.class);
provider = LocationManager.GPS_PROVIDER;
listener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Log.e("UPDATE","UPDATING LOCATION");
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
};
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
The requestLocationUpdates method is called but the onLocationChanged method is only called a few times. GPS is activated and the permissions are granted.
Do you have any idea why onLocationChanged is not always called? I also checked getLastKnownLocation which is sometimes false after calling requestLocationUpdates.
I think onLocationChanged() is not suppose to be called always, it will only be called if a location changes.
I changed the request method to lm.requestSingleUpdate(criteria, listener,null); which works perfect as well as requestLocationUpdates with criteria.
Seems to be a problem with the provider.
Related
I receive crush, when user turn off the gps.
I have code like this to detect location`s user
if(locationManager == null) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L,
15f, mLocationListener);
} else if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000L,
15f, mLocationListener);
} else {
Toast.makeText(this, "Please turn on gps on ypur phone", Toast.LENGTH_SHORT).show();
}
When user open app with gps, then turn off it, I receive crush like this
java.lang.AbstractMethodError: abstract method "void android.location.LocationListener.onProviderDisabled(java.lang.String)"
I tried to detect when user turn off gps, but I get crush earlier, then receiver detect
Find the declaration of mLocationListerner. Surely it overrite onLocationChanged(). But you may have forgotten override onProviderDisabled(). Do it even if it's empty. Override also onStatusChanged() and onProviderEnabled().
Luck.
LocationListener mLocationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
....
}
#Override
public void onProviderDisabled(String provider) { }
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
#Override
public void onProviderEnabled(String provider) { }
};
Lets say that i have an object that asynchronously will be seted (an user GPS position for example).
Is there a way to only call a method in an Activity when this object is not null?
If you simply want to call a method if some object is not null then:
if (myObject != null) { callMyActivityMethod()}
Based on your updated information, you only want to call the method once you have received a location from the GPS.
A LocationListener is used for receiving notifications from your LocationManager.
Create a LocationListener and pass it in your requestForLocationUpdates(). It will then notify you when a new location has been received. For Example:
LocationListener locListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
callMyActivityMethod();
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
};
...
requestLocationUpdates(provider, minTime, minDistance, locListener);
...
when used GPS_PROVIDER the application work fine, but when used NETWORK_PROVIDER the application was stopped by force. Why? are my permissions not complete?? please help me by making my code working, thanx
public class My_location extends Activity implements LocationListener {
EditText ET1,ET2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_location);
// Log.i("onCreate_My_location", "begin");
ET1=(EditText)this.findViewById(R.id.editText1);
ET2=(EditText)this.findViewById(R.id.editText2);
ET1.setText("1.0000");
ET2.setText("1.0000");
LocationManager LM1=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
LM1.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,1,this);
//Log.i("onCreate_my_location", "End");
}
#Override
public void onLocationChanged(Location L1) {
// TODO Auto-generated method stub
//Log.i("onLocationChanged",String.valueOf(L1.getLongitude()));
//Log.i("onLocationChanged",String.valueOf(L1.getLatitude()));
//Log.i("onLocationChanged",String.valueOf(L1.getAltitude()));
ET1.setText("C.0000");
ET2.setText("C.0000");
ET1.setText(String.valueOf(L1.getLongitude()));
ET2.setText(String.valueOf(L1.getLatitude()));
}
#Override
public void onProviderDisabled(String arg0) {
}
#Override
public void onProviderEnabled(String arg0) {
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
}
instead of using specific provider use best provider, which will give you more accuracy in your location
use code given below which will give you location by choosing best provider automatically :
LocationManager LM1=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider=LM1.getBestProvider(criteria, true);
LM1.requestLocationUpdates(provider,0,1,this);
Edit:
for Your Code you Can Check network State before requestLocationUpdates
i think this can avoid the crash :
if(LM1.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
{
LM1.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,1,this);
}
Check whether the network location provider is enabled or not.
try {
network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {
ex.printStackTrace();
}
If the provider is enabled then only you can use that. Also, for location updates using a network provider, I think giving the minDistance value as 1 is meaningless as coarse location is anyway not very accurate. You might want to change that value to 50 or 100 meters.
I am trying to get the location in my Android app as follows
LocationManager lmanager = (LocationManager)context.GetSystemService(Context.LocationService);
if (lmanager.IsProviderEnabled(LocationManager.NetworkProvider))
{
Location currentLoc = lmanager.GetLastKnownLocation(LocationManager.NetworkProvider);
}
I tried with LocationManager.gpsProvider also instead of the Network provider.
In all the cases I am getting a null reference exception for currentLoc.
The GetLastKnownLocation() method returns the last location it knew about, which could be null if it has never received any updates before. I think what you want to do here is listen for location updates, and then respond to them once they come in. You can do this by implementing the ILocationListener interface:
class MyLocationListener : Java.Lang.Object, ILocationListener
{
public void OnLocationChanged(Location location)
{
}
public void OnProviderDisabled(string provider)
{
}
public void OnProviderEnabled(string provider)
{
}
public void OnStatusChanged(string provider, Availability status, Bundle extras)
{
}
}
In your activity you can declare that you'd like to use this class to start listening for updates like this:
var locationManager = (LocationManager)GetSystemService(LocationService);
var locationListener = new MyLocationListener();
locationManager.RequestLocationUpdates(LocationManager.NetworkProvider, 5000, 2, locationListener);
this is the requestLocationUpdates call I am using, 900000 (15 minutes) and 500 meters shouldn't be causing the gps to fire constantly, its really draining battery...
I know this code isn't helpful.. But does anyone know why this might be happening?
in my onStartCommand
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 900000, 500, locationListener);
mLocation = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
and then
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
mLocation = location;
}
#Override
public void onProviderDisabled(String arg0) {}
#Override
public void onProviderEnabled(String arg0) {}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
};
The time parameter to requestLocationUpdates() is a hint, not a rule -- you may get locations more frequently than this.
If you want to stop consuming battery due to having the GPS on, call removeUpdates().