Android GPS 仅在 Google 地图运行后进行通信

发布于 2024-09-11 05:32:15 字数 887 浏览 7 评论 0原文

我寻找答案,但没有找到答案 - 我的问题如下:

我编写了与模拟器一起使用的代码(我通过 DDMS 发送本地坐标)。

该代码也适用于设备但是,只有在运行 Google 地图之后。我猜清单或代码中缺少一些东西,但我不知道它是什么,我可以使用一些建议/建议。

以下是代码相关部分的片段: (我无法弄清楚如何正确嵌入这些东西,所以它看起来很智能/有组织,但是,我认为你明白了......)

homeboy = (LocationManager)getSystemService(Context.LOCATION_SERVICE);    
locationListener = new MyLocationListener();
Location recentLoc = homeboy.getLastKnownLocation(LocationManager.GPS_PROVIDER);

I also have a LocationListener method (shown w/o it's four methods of checking for enabled, changed,...etc)
private class MyLocationListener implements LocationListener {
... etc.

I include this in the manifest file near the top:
<uses-permission 
android:name="android.permission.ACCESS_FINE_LOCATION" />

那么,缺少什么? 另外,如何从 wifi 获取位置(例如通过任何可用的方式获取位置,除了将最后一个位置保存到文件并检索它之外)。

感谢您的任何意见

I looked for the answer but didn't find one - my question(s) follow:

I wrote code that works with the emulator (I send local coords vis DDMS).

The code also works on the device BUT, only after running Google Map. I'm guessing there's something missing in either the manifest or in the code but I don't know what it is and I could use some advice/suggestions.

Here are snippets of the relevant portions of the Code:
(I wasn't able to figure out how to properly embed this stuff so it looked intelligent/organized but, I think you get the point...)

homeboy = (LocationManager)getSystemService(Context.LOCATION_SERVICE);    
locationListener = new MyLocationListener();
Location recentLoc = homeboy.getLastKnownLocation(LocationManager.GPS_PROVIDER);

I also have a LocationListener method (shown w/o it's four methods of checking for enabled, changed,...etc)
private class MyLocationListener implements LocationListener {
... etc.

I include this in the manifest file near the top:
<uses-permission 
android:name="android.permission.ACCESS_FINE_LOCATION" />

So, what's missing?
Also, how to get location from wifi (as in getting the location by any means available short of saving the last location to a file and retrieving it).

Thanks for any input

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似最初 2024-09-18 05:32:15

那么,还缺少什么?

您没有执行任何操作来打开 GPS。 GPS 无线电大部分时间都是关闭的,因为它非常耗电。因此,getLastKnownLocation() 大多数时候都会返回 null。使用 requestLocationUpdates() 或其他方法让 Android 打开 GPS。

So, what's missing?

You are not doing anything to turn on GPS. The GPS radio is off most of the time, because it is a battery hog. Hence, getLastKnownLocation() returns null most of the time. Use requestLocationUpdates() or something to get Android to turn on GPS.

寻梦旅人 2024-09-18 05:32:15

我通过“设置”打开 GPS。难道不应该注意打开它吗?我在运行我的应用程序之前将其打开(也尝试等待几分钟)。

如果不回答我自己的问题,我似乎无法回应你(任何)的答案......

I turn the GPS on via the Settings. Shouldn't that take care of turning it on? I turn it on before running my app (tried waiting a few minutes, too).

I seem not able to respond to your (any) answer without answering my own question....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文