I've a TI OMAP platform running Kitkat 4.4.4. The platform doesn't have Wifi/GPS/Cellular connection but it has ethernet. I use eth1 to access internet.
Is there anyway to determine the coarse geographical location of the device based on eth1. I tried the sample code below, but it fails for ethernet connection.
/** Check if we can get our location */
public void checkLocation(){
// Get the location manager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String locationProvider = LocationManager.NETWORK_PROVIDER;
try{
// Define a listener that responds to location updates
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.d(TAG,"Latitude: " + location.getLatitude() + ", Longitude = " + location.getLongitude());
}
public void onStatusChanged(String provider, int status, Bundle extras) {Log.d(TAG,"location found 1");}
public void onProviderEnabled(String provider) {Log.d(TAG,"location found 2");}
public void onProviderDisabled(String provider) {Log.d(TAG,"location found 3");}
};
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
}catch(Exception e){
Log.e(TAG, "Location Exception: " + e.getMessage());
}
}
Above code throws provider doesn't exist: network exception for ethernet. Works OK on a device with wifi/GPS.
I've also set the following permissions in manifest file.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Any ideas how to determine location if the device has on ethernet connectivity?
Thanks
You can use
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
and
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
to check the availability of your network or gps. If these two return false to you, then you might not be able to use the locationManager to find location.
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);
I am trying to use the following code snippet on an Android app with targetSdkVersion = 22. (source)
// 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.
System.out.println(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);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
In my manifest permissions, I have:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.location.network" />
Under Settings -> Location, my phone (Galaxy S8) has three locating methods:
High accuracy, Battery Saving and Phone only. This code successfully works for the first 2 locating methods but not Phone only.
Phone only says it "Uses GPS to estimate your location", so I would expect using the GPS_PROVIDER would be sufficient to get location changes.
Why isn't it working?
I have an app that listens to the GPS update, which was implemented last year using API 21. It was tested on several Android phones running Lollipop, such as LG G4. Now I installed it on an LG G5 with Marshmallow and I receive no GPS updates, i.e. onLocationChanged() is never called.
The permissions are granted (the App view on the phone's settings shows Location permission) and the following statement is true
checkCallingOrSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
AndroidManifest.xml includes these lines:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
The code I am using is as below:
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = getLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
private LocationListener getLocationListener() {
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
handleLocationChange();
}
public void onStatusChanged(String provider, int status, Bundle extras) { }
public void onProviderEnabled(String provider) { }
public void onProviderDisabled(String provider) { }
};
return locationListener;
}
I tried NETWORK_PROVIDER as well, no success.
What am I missing here?
I realized that the GPS Sensor is not providing any updates to the phone. Such as, when I restrict the Location precision to the GPS Sensor only, Google Maps never get a location fix.
If I connect the phone to the internet, then the NETWORK_PROVIDER provides location updates.
I want to get current latitude and longitude of device even when it is not in movement.
Tried both requestLocationUpdates() and requestSingleUpdate() but my location is still null. LocationManager.onLocationChanged() seems to be called only when location gets changed. I want to post this location details but fails with NullPointerException every time.
Here are my code samples:
Activity:
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);
if (location == null) {
try {
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, myLocationListener, Looper.myLooper());
} catch (SecurityException e) {
}
}
//MyLocationListener
public void onLocationChanged(Location location) {
activity.setLocation(location);
}
}
}
Manifest:
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
If the user has not used GPS for a while, there will be no last known location, hence the GPS provider will not update at all unless you move outside. Its better to use the new FusedLocationApi to get updates as it selects the best source for you and gives you the best possible location.
https://developer.android.com/training/location/receive-location-updates.html
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
Trying putting your code in AsyncTask.
I got a weird problem. I uses wifi to retrieve user location. I have tested on several phones; on some of them, I could never get a location update. it seemed that the method onLocationChanged is never called. My code is attached below.. Any suggestions appreciated!!!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.NETWORK_PROVIDER;
Location l = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(l);
locationManager.requestLocationUpdates(provider, 0, 0,
locationListener);
}
private void updateWithNewLocation(Location location) {
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
String latLongString = "No location found";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
}
myLocationText.setText("Your Current Position is:\n" +
latLongString);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
Log.i("onLocationChanged", "onLocationChanged");
Toast.makeText(getApplicationContext(), "location changed",
Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status,
Bundle extras) {}
};
Manifest
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
My guess is that the phones on which you don't get location updates from do not have proper data connection. Network Provider needs GSM/3G or WiFi data connection to retrieve a location fix from Google servers using phones's cell/wifi data. You can use the method below to check this.
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
Please check that your onProviderDisabled method is not being called immediately.
If you find it is being called as soon as your provider registers, it means that the phone you're on has location services disabled.
It's my suspicion now that Google Location Services were down in select areas. My app started working again with no changes to the code (the apk from the Play Store).
I too faced the same issue with NetworkProvider while testing in ICS(4.0) but working fine in older versions(2.2 and 2.3.3 i tested). Here is the solution i found and working fine in ICS as well. Please use the Criteria for getting the location updates as below:
LocationManager locationManager= (LocationManager) <YourAct>
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE); //ACCURACY_COARSE or ACCURACY_FINE based on usage
String provider = locationManager.getBestProvider(criteria, true);
// Register the listener with the Location Manager to receive location
// updates
locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
Replace the code as above. This will help u in getting the location updates.
I had the very same problem and in turns out that onLocationChanged is not called if there is getLastKnownLocation which is the same as "current". That means that you will not get new location because there is nothing new to report...
Solution is to use the last known location even it is very old (and continue to listen for updates if you need more precise/new location (if such occurres)).