How to get location without internet using intent service? - android

I don't want to use service which is continuously running in background ! Actually in my project user will send SMS command from any smartphone to his/her misplaced smartphone.My app will detect that particular "SMS command" and in return it will send the current location of misplaced mobile.
can it be done through intent service ? I m damn confused ... Its single time operation how to perform it efficiently ... ?

by using NETWORK_PROVIDER getting geolocation
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);
and add this permission in manifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Related

Using LocationListener for API 21 on Marshmallow

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.

Geolocation of android device

How can I get continuous geo-location of android device programmatically ?
I'm having troubles of getting position coordinates continuously of android device and show the navigation move according to the geolocation.
According to Android documentation :
// 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);
More information available on their page : http://developer.android.com/guide/topics/location/strategies.html

Getting the Location even if LocationServices is turned off

In Android if LocationServices is disabled, is there any way that I can get the current location through wifi/network. I know there is one way of showing the user a popup to change his LocationSettings , but I don't want to show the popup.
So is there any way of getting the current location even if LocationServices is disabled ?
use LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER
The entire code and how to use is given here :
http://developer.android.com/guide/topics/location/strategies.html
// 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);
Yes of course, you can use:
// let Android select the right location provider for you
String myProvider = locationManager.getBestProvider(myCriteria, true);
check this for more details good-way-of-getting-the-users-location-in-android and this Location Strategies

Getting 0.0 for longitude and latitude on tablet

I am getting longitude=0.0 and latitude=0.0 on tablet while this is working perfectly on the phone.
I am using LocationManager.NETWORK_PROVIDER and not the GPS_PROVIDER so what could be the cause please?
Logcat output??
that's my code:
// Acquire a reference to the system Location Manager
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
longitude=location.getLongitude();
latitude=location.getLatitude();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
//Or use LocationManager.GPS_PROVIDER
String locationProvider = LocationManager.NETWORK_PROVIDER;
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
if(lastKnownLocation!=null){
longitude=lastKnownLocation.getLongitude();
latitude=lastKnownLocation.getLatitude();
}
Check out Location settings inside your device settings. This seems not a problem with your code, but your device settings. In my Ginger bread, it's as:
Settings -> Location&security -> Use wireless networks

why my gps is not triggered?

i have a problem with my app getting my GPS position.
first of all i have the two things in my manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
then i have a code like this for the location:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
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) {}
};
locationManager.requestLocationUpdates(0l, 0f, new Criteria(), locationListener, Looper.getMainLooper());
now the strange thing:
when i have wifi enabled and i'm in my network, i get a position. If i have only GPS enabled i dont get my position. even i dont see the gps icon at the top of the phone which shows when a app makes use of my gps.
what is wrong here?
For GPS:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
For Network:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

Categories

Resources