Get location of android device automatically - android

I'm working on an android app and i need to know the location of android device. All the methods I tried need GPS to be enabled. If it is not enabled, we can ask the user to turn it on by opening settings intent.
Is there any way to enable it via code, without even letting the user know.. and turn it off when the app has finished its job?
The app I'm working on will keep record of location of device. It will run in background and will note location every 15mins. To keep GPS enabled all the time will consume a lot of power. So i want to turn it on when i need and turn it off when i'm done, without asking the user.

For exact location, you need GPS to be enabled. But you can use NETWORK_PROVIDER, and get the latitude and longitude. This does not require user intervention. But, of course, its an approximate location but good enough to do location based searches.
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListener);

For privacy reasons, that will never be possible without rooting the device.
You cannot get the user's location without consent.

Is there any way to enable it via code, without even letting the user know.. and turn it off when the app has finished its job?
Fortunately, no, for obvious privacy and security reasons.
So i want to turn it on when i need and turn it off when i'm done, without asking the user.
On and off are not the same as enabled and disabled.
Just because the user has allowed GPS to be enabled does not mean that it is drawing power. Only if an app has requested location data will the GPS radio be turned on. So, you cannot enable GPS (only users can), but you can arrange for GPS to be turned on if it is enabled. The user will still see an icon in the status bar when GPS is on.
How does the spy apps and all do that then?
They require that GPS be enabled by the user.

Related

How to programmatically turn on GPS on Android

I have survey some article on stackoverflow.
It seems android version above 4.0 is not able to turn on/off GPS programmatically.
Is there anything changed now? Or is this still not possible?
You can't turn it on/off programatically due to security reasons. According to Change location settings docs:
If your app needs to request location or receive permission updates, the device needs to enable the appropriate system settings, such as GPS or Wi-Fi scanning. Rather than directly enabling services such as the device's GPS, your app specifies the required level of accuracy/power consumption and desired update interval, and the device automatically makes the appropriate changes to system settings.
So, the best thing to do is, if the user has disabled the GPS, then for better UX you can show a prompt and redirect the user to the Location activity like this:
MainActivity.Instance.StartActivity(new Intent(ActionLocationSourceSettings));
Where MainActivity.Instance is your current Activity.

FusedLocationProvider finds location faster with GPS turned of

My android app relies on location. However the FusedLocationProvider which I use for this as abstraction layer so that I do not have to worry about what LocationProvider to use, behaves unexpectedly.
Scenario:
User opens app. I check for the availability of the providers and open the location settings page. The user enables "Access to my location" and both GPS and Wi-Fi location. When the user returns to the app I receive no location updates.
When I leave GPS turned off and only allow Wi-Fi in the System settings, I get an updated location.
I tried the priorities PRIORITY_LOW_POWER and PRIORITY_BALANCED_POWER_ACCURACY which both did not give me a location update when GPS was turned off.
How does it come, that the detection of the location works with GPS turned off and doesn't when it is turned on? Does that make any sense?
The location I obtain, when GPS is turned off is accurate enough for what the app is doing. If the device has had LocationServices turned on for a longer periods of time (Including the phone being outside) I receive Location Updates even with GPS turned on.
Does someone have an explanation for this / know how to solve this?

Android show android natice pop up settings/cancel to enable location service

I am working on an android application that uses geo location services, when the user accesses such service if location access is disabled on the device i want to call android native pop up that guides user to enabled location access. Is it possible to do this ?
I noticed there's even better way to achieve good location settings, without "guiding" user to turn them on, mentioned in this question:
Enabling Location with mode High Accuracy or Battery saving without user needing to visit Settings
Since Google Play Services 7.0 you can ask system to offer user turning the settings on with one touch. It's how Google Maps are doing it.

How to stop fake locations using LocationClient on Android

I'm working on a GPS game, similar to geocaching, which requires the devices accurate GPS cordinates.
I am using LocationClient and not LocationManager.
Early in testing, a friend pointed out that the location can be faked using free apps on the play store. A bit of research shows that without root, these apps require Mock Location to be enabled. So I started with a simple check to see if mock location was enabled, and if it was then the app would not allow the user to play until it is disabled.
This got me thinking though, users who are rooted could still fake their location without having mock location enabled, however most of these apps still have the MOCK_LOCATION permission regardless if the device is rooted or not. So I used a method that detects any apps that require Mock Location. The problem is, even on my non rooted Note 3, there appears to be several pre-installed apps that require Mock Location for some reason, which then prevents game play. So this solution wont work.
Is there any other way to check if a location might be faked using LocationClient?
In API 18+ you can use Location.isFromMockProvider to check if a particular location is mocked. This flag will be set to true if an app is providing mocked (fake) locations or if you are using a mock provider in your own app.
Of course, rooted phones can bypass this.
In your device, go to settings >> developer options >> allow mock locations
This option must be checked in you device that's why you are getting mock location updates. To stop receiving just uncheck it.
In your code, you can also set mLocationClinet.setMockMode(false); in your onCreate() method of Location tracking activity.
I hope it works :)

why do apps show a GPS start button?

If you look at apps such as Google Maps app on Android, it display a gps icon, that when clicked starts the gps. However, if I don't click that image, and I move, it tracks me anyway. So what is the purpose of they button which we see in so many apps? Does the phone rely on GPS only when that button is pressed, or do they use the button, because they start the gps every x minutes, so if you click the button, it start it immediately?
?
The GPS radio is not started automatically.
When the GPS is off, the phone uses nearby WiFi nodes to find the location. That works pretty well in areas where there are a lot of known WiFi nodes, but the GPS is more exact, and works pretty much everywhere.
Applications like google maps have the gps logo because you may want to be shown your location, usually the application will try and find your location based on wifi and networks first and then using gps. The button will tell the app you want to know the location then and there and that's why it starts, other then that it should just do it automatically.
GPS isn't the only way to track a phone. When you don't have GPS enabled (or if your phone doesn't have a GPS chip in it), the cell phone can triangulate it's distance from the nearby cell towers. It can also do the same thing with nearby WiFi networks.
Since GPS takes up so much power, apps will ask you to enable GPS yourself instead of automatically enabling it when the app starts/runs in the background.
You can read more about common phone tracking methods here: http://en.wikipedia.org/wiki/Mobile_phone_tracking

Categories

Resources