SureshS
SureshS

Reputation: 609

Get location of android device automatically

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.

Upvotes: 0

Views: 1663

Answers (3)

rahul
rahul

Reputation: 6487

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);

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006584

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.

Upvotes: 0

SLaks
SLaks

Reputation: 887275

For privacy reasons, that will never be possible without rooting the device.

You cannot get the user's location without consent.

Upvotes: 0

Related Questions