BroadcastReceiver for Google Play Location Service (setting of the Status Bar) - android

I'm trying to register a BroadcastReceiver for get the state changes of Location settings
But I didn't find any documentation for doing it. I only found BroadcastReceiver to check the status of certain search providers (GPS and Network providers); but not for checking if this particular option (Location) is active or not in the system preferences.
Somebody can show me the right direction?
NOTE:
I used Google Play Location Service (com.google.android.gms.location.LocationListener interface).

Well, I found one way to check if the "Location" setting of the action bar is enabled or disabled; but without a BroadcastReceiver (a shame, really)
private static final String TAG = getClass().getName();
/* We define the LocationManager */
LocationManager location_Manager= (LocationManager) getSystemService(LOCATION_SERVICE);
/* If the GPS provider or the network provider are enabled; the Location setting is enabled.*/
if(location_Manager.isProviderEnabled(LocationManager.GPS_PROVIDER) || location_Manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Log.d(TAG, "The Location setting is enabled");
}else{
/* We send the user to the "Location activity" to enable the setting */
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
I really did not need a BroadcastReceiver; since the "arquitecture" that has my app allows me to do without it; but I would have liked to know how to use it.
NOTICE:
If someone finds the way to make it with a BroadcastReceiver I will change my correct answer by her answer.

You can try to use Location Listener.
onProviderDisabled(String provider)
Called when the provider is disabled by the user.
Compare to provider String such as
provider.equalsIgnoreCase(LocationManager.GPS_PROVIDER)
http://developer.android.com/reference/android/location/LocationListener.html

Related

Simplest way to get rough user location in Android [duplicate]

Is it possible to get the current location of user without using GPS or the internet? I mean with the help of mobile network provider.
What you are looking to do is get the position using the LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER. The NETWORK_PROVIDER will resolve on the GSM or wifi, which ever available. Obviously with wifi off, GSM will be used. Keep in mind that using the cell network is accurate to basically 500m.
http://developer.android.com/guide/topics/location/obtaining-user-location.html has some really great information and sample code.
After you get done with most of the code in OnCreate(), add this:
// 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);
You could also have your activity implement the LocationListener class and thus implement onLocationChanged() in your activity.
By getting the getLastKnownLocation you do not actually initiate a fix yourself.
Be aware that this could start the provider, but if the user has ever gotten a location before, I don't think it will. The docs aren't really too clear on this.
According to the docs getLastKnownLocation:
Returns a Location indicating the data from the last known location
fix obtained from the given provider. This can be done without
starting the provider.
Here is a quick snippet:
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import java.util.List;
public class UtilLocation {
public static Location getLastKnownLoaction(boolean enabledProvidersOnly, Context context){
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location utilLocation = null;
List<String> providers = manager.getProviders(enabledProvidersOnly);
for(String provider : providers){
utilLocation = manager.getLastKnownLocation(provider);
if(utilLocation != null) return utilLocation;
}
return null;
}
}
You also have to add new permission to AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
No, you cannot currently get location without using GPS or internet.
Location techniques based on WiFi, Cellular, or Bluetooth work with the help of a large database that is constantly being updated. A device scans for transmitter IDs and then sends these in a query through the internet to a service such as Google, Apple, or Skyhook. That service responds with a location based on previous wireless surveys from known locations. Without internet access, you have to have a local copy of such a database and keep this up to date. For global usage, this is very impractical.
Theoretically, a mobile provider could provide local data service only but no access to the internet, and then answer location queries from mobile devices. Mobile providers don't do this; no one wants to pay for this kind of restricted data access. If you have data service through your mobile provider, then you have internet access.
In short, using LocationManager.NETWORK_PROVIDER or android.hardware.location.network to get location requires use of the internet.
Using the last known position requires you to have had GPS or internet access very recently. If you just had internet, presumably you can adjust your position or settings to get internet again. If your device has not had GPS or internet access, the last known position feature will not help you.
Without GPS or internet, you could:
Take pictures of the night sky and use the current time to estimate your location based on a star chart. This would probably require additional equipment to ensure that the angles for your pictures are correctly measured.
Use an accelerometer to track location starting from a known position. The accumulation of error in this kind of approach makes it impractical for most situations.
boolean gps_enabled = false;
boolean network_enabled = false;
LocationManager lm = (LocationManager) mCtx
.getSystemService(Context.LOCATION_SERVICE);
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Location net_loc = null, gps_loc = null, finalLoc = null;
if (gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (gps_loc != null && net_loc != null) {
//smaller the number more accurate result will
if (gps_loc.getAccuracy() > net_loc.getAccuracy())
finalLoc = net_loc;
else
finalLoc = gps_loc;
// I used this just to get an idea (if both avail, its upto you which you want to take as I've taken location with more accuracy)
} else {
if (gps_loc != null) {
finalLoc = gps_loc;
} else if (net_loc != null) {
finalLoc = net_loc;
}
}
Here possible to get the User current location Without the use of GPS and Network Provider.
1 . Convert cellLocation to real location (Latitude and Longitude), using "http://www.google.com/glm/mmap"
2.Click Here For Your Reference
Have you take a look Google Maps Geolocation Api? Google Map Geolocation
This is simple RestApi, you just need POST a request, the the service will return a location with accuracy in meters.
It appears that it is possible to track a smart phone without using GPS.
Sources:
Primary: "PinMe: Tracking a Smartphone User around the World"
Secondary: "How to Track a Cellphone Without GPS—or Consent"
I have not yet found a link to the team's final code. When I do I will post, if another has not done so.
You can use TelephonyManager to do that .

Get information of Location Services enabled/disabled using Fused Location Provider

I am working on an app which uses Google Maps and tracks user's location. I want to change the visibility of the "You are here!" marker when the user closes Location Services by hand or services goes to condition of inaccessible. This method can return the information of whether Location Services enabled or not:
private boolean isLocationServicesEnabled() {
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
return true;
}
return false;
}
However, I do not want to detect the status of Location Services for once. Whenever the user opens or closes Location Services, I should detect it and change the visibility of marker. In short, this sloppy pseudocode explains what I want to listen:
while application runs
if location services turn off
change marker visibility to false
else
change marker visibility to true
I did not find a way to achieve this task without android.location.LocationListener, want to achieve it just using Fused Location Provider. Do you have any idea? Here is the core structure I used if you want to see:
http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/
(Check the title of "Complete Code")
Doing this the proper way involves a huge amount of code. You have to make use of the location SettingsApi class.
The main entry point for interacting with the location
settings-enabler APIs.
This API makes it easy for an app to ensure that the device's system
settings are properly configured for the app's location needs.
Fortunately there is a full blown sample provided by Google on github
How about implementing a gps listener?
mLocationManager.addGpsStatusListener(new GpsStatus.Listener(){
#Override
public void onGpsStatusChanged(int event) {
if(event==GpsStatus.GPS_EVENT_STARTED){
Log.d(TAG,"Gps Started");
}else if(event==GpsStatus.GPS_EVENT_STOPPED){
Log.d(TAG,"Gps Stopped");
}
}
});
modify the above code to change visibility of your marker. Hope this works.

Android: changing the location provider dynamically

In my app, I need to get the location of my users. Since my app is going to do it constantly, I want to change the location provider in certain conditions (between GPS and NETWORK providers). My app is going to detect such conditions and change it.
But, I'm not sure how to change it dynamically.
So far, I have a thread (that, among other things, will check for these conditions) and I want it to change the location provider too. My code until now is as below:
public void run() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationController = new LocationController();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, time, desltaDistance, locationController );
while (serviceIsRunning) {
...
if (conditionsAreMet) {
locationManager.removeUpdates(locationController);
if (provider == LocationManager.GPS_PROVIDER)
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, time, desltaDistance, locationController );
else
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, deltaDistance, locationController );
}
...
}
}
*The LocationController class implements LocationListener.
But, it don't seems to be the better way to do it. So, what I want to know is: there is a better way to change the location provider automatically?
Since no one answered this question, here is how I figured it out.
Instead of make the change between the providers, I registered a constant controller for the NETWORK_PROVIDER. And, according to my conditions, a controller for the GPS_PROVIDER may be registered or not.
It's easier to register/unregister the locations request for one provider, instead of keep changing between them.

android requestLocationUpdates failing

I am developing a proximity alert related project. For that whenever I opened my app, I need to get exact readings of where I am. Even though I am following the right coding practices prescribed by android documentation, I am not getting the expected result.
Why there is no alternative command in entire android Geolocation coding for getLastKnownLocation which will give us where we are earlier not now.
I have did one javascript coding in the same lines. There My code is working properly. The Descriptive address and coordinates where my device is working nice there. Those commands getCurrentPosition and watchPosition are givinga beautiful response via their event handler callbacks. Why there is no getCurrentLocation in android geolocation parlance?
Even though I have followed relevant coding practices, MyLocationListener myLocationUpdate which implements LocationListener is not updating my new location when I am moving from one place to another place. I gave MINIMUM_DISTANCE_CHANGE_FOR_UPDATES as 1(in meters) and MINIMUM_TIME_BETWEEN_UPDATES as 1000 (in milliseconds).
I am giving important code snippets below to understand the problem
in onCreate handler of the activity
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
myLocationUpdate = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, myLocationUpdate);
retrieveLocationButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"Finding Location",Toast.LENGTH_LONG).show();
showCurrentLocation();
}
});
latituteField = (TextView) findViewById(R.id.display_Location);
showCurrentLocation();
in showCurrentLocation function
I am using locationManager.getLastKnownLocation(provider) to retrieving that location.
By using GeoCoder Object and the command geocoder.getFromLocation(latitude, longitude, 1) to get First Address match for the coordinates.
// internal class to handle location cahnge event
private class MyLocationListener implements LocationListener contains all the Overridden functions including public void onLocationChanged(Location location)
But Practically I am getting nothing out of all the application. I have already recorded Time via location.getTime(). It is showing a fixed earlier time, but not the interval i specified.
the problem with getting GPS location is that it isnt available immediately. From my understanding of GPS location provider is that when you request location update, the gpr provider will try to connect to the gps satellites which runs in a separate thread (not entirely sure about it). In the meantime your program is executed normally and there maybe a chance that you wont get any location.
What you can do is use Fused Location Provide which was introduced in this year's IO Event. You can find the tutorial here

How can I check for GPS support in-App to add a feature for those with Location services enabled?

How can I check for GPS support in-App to add a feature for those with Location services enabled?
My concern is, I know I'd have to specify the tag in the manifest to declare that the app uses location services, but I still want the app to function for those without. I just want to check and, if the service is available, use it; otherwise just ignore that one feature.
Thanks.
You can check if any Location services are enabled by looking to see if any location providers are enabled. I'm using this function in my code right now:
public static boolean areProvidersEnabled(Context context) {
ContentResolver cr = context.getContentResolver();
String providersAllowed = Settings.Secure.getString(cr, Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return providersAllowed != null && providersAllowed.length() > 0;
}
Another neat thing is that you can send the user straight to the Location settings and ask them if they want to enable it. I'll leave it to you on how to ask the user, but the Intent to get to the settings is like this:
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivity(intent);

Categories

Resources