简体   繁体   English

自动获取android设备的位置

[英]Get location of android device automatically

I'm working on an android app and i need to know the location of android device. 我正在开发一个android应用程序,我需要知道android设备的位置。 All the methods I tried need GPS to be enabled. 我尝试的所有方法都需要启用GPS。 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. 它将在后台运行,每15分钟记录一次位置。 To keep GPS enabled all the time will consume a lot of power. 一直保持GPS启用将消耗大量功率。 So i want to turn it on when i need and turn it off when i'm done, without asking the user. 因此,我想在需要时将其打开,在完成后将其关闭,而无需询问用户。

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. 仅仅因为用户允许启用 GPS并不意味着它正在消耗能量。 Only if an app has requested location data will the GPS radio be turned on. 仅当应用请求了位置数据时,GPS无线电才会打开。 So, you cannot enable GPS (only users can), but you can arrange for GPS to be turned on if it is enabled. 因此,您无法启用 GPS(只有用户才能启用 ),但是如果启用 GPS,则可以安排将其打开。 The user will still see an icon in the status bar when GPS is on. GPS开启时,用户仍将在状态栏中看到一个图标。

How does the spy apps and all do that then? 间谍应用程序以及所有这些应用程序是如何做到的?

They require that GPS be enabled by the user. 他们要求用户启用GPS。

For exact location, you need GPS to be enabled. 为了获得准确的位置,您需要启用GPS。 But you can use NETWORK_PROVIDER, and get the latitude and longitude. 但是您可以使用NETWORK_PROVIDER,并获取经度和纬度。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM