Android的GPS不工作,直到重新启动新手机 [英] Android GPS does not work until restart on new phones

查看:243
本文介绍了Android的GPS不工作,直到重新启动新手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经张贴了关于这个问题前,发现谁曾没有解决同一问题的其他几个人发现了。

I have posted about this issue before, and found a few other people who have had the same issue with no solutions found.

我开发一个Android应用程序,提交一个JSON查询到服务器所获得的GPS坐标和地理codeD邮编code。对于全新的用户从未下载的应用程序,GPS不任何工作。它不是直到重新启动手机,GP​​S将正常工作。安装该应用程序,然后重新启动后,GPS就可以正常工作,每次没有问题,即使他们再次重启。

I am developing an Android app that submits a JSON query to a server with the obtained GPS coordinates and geocoded Zip Code. For brand new users that have never downloaded the app, GPS does not work whatsoever. It is not until rebooting the phone that GPS will work. After installing the app and then rebooting, the GPS will work every time without problem, even if they restart again.

有precious资料很少在这个问题上,我已经找到了唯一的问题是指使用谷歌播放位置服务,没有进一步的细节。任何人都有这个问题?我的发展完全停止,直到这个问题就可以解决。


编辑: 这里是链接到 MainActivity.java 调用地理定位6功能的文件。

There is precious little information on this issue, and the only issue I have found refers to using Google Play Location Services, with no further details. Has anyone else had this issue? My development is completely halted until this issue can be resolved.


Here is the link to the MainActivity.java file that calls the geopositioning functions.

下面是链接到 MyLocation.java 文件,它包含了地理位置的多源的实际逻辑

Here is the link to the MyLocation.java file that contains the actual logic for multiple sources of geolocation

我毫不怀疑,这样做有全球定位系统的更好的方法。我是很新的Andr​​oid开发,所以在这方面的任何帮助是非常AP preciated。

I have no doubt that there are much better ways of doing GPS. I'm very new to Android development, so any help on this front is very much appreciated.

编辑2: 我已经抹去我的手机恢复出厂设置,并从头开始。我仍然无法在此设备上复制的问题,只有在使用的第一次之前,重新启动该应用程序的手机。

EDIT 2: I have wiped my phone with a factory reset, and started from scratch. I still cannot replicate the issue on this device, only on phones using the app for the very first time prior to a restart.

推荐答案

看起来像你注册了这两个在全球定位系统和网络提供商监听位置10秒,当计时器10秒后熄灭尝试来自供应商获得最新的位置。

Looks like you're registering both the GPS and NETWORK providers to listen for a location for 10 seconds, and when the timer goes off after 10 seconds you try to get the most recent location from both providers.

有几件事会在这里。

首先,你似乎在倾听,在错误的方法更新。你的两个监听器应该是这样的:

First, you seem to be listening for updates in the wrong method. Your two listeners should look like:

    LocationListener locationListenerGps = new LocationListener() {

            // This will never be called, its not part of the LocationListener interface - http://developer.android.com/reference/android/location/LocationListener.html
            /* public void onStatusChanged(Location location) {
                    timer1.cancel();

                    locationResult.gotLocation(location);
                    lm.removeUpdates(this);
                    lm.removeUpdates(locationListenerNetwork);
            } */
            public void onProviderEnabled(String provider) {}
            public void onProviderDisabled(String provider) {}
            public void onLocationChanged(Location location) {
                    // This is the correct method to receive location callbacks
                    timer1.cancel();

                    locationResult.gotLocation(location);
                    lm.removeUpdates(this);
                    lm.removeUpdates(locationListenerNetwork);

            }
            public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

二,我会使用的ScheduledThreadPoolExecutor 或< A HREF =htt​​p://developer.android.com/reference/android/os/Handler.html相对=nofollow>处理器代替的定时器

定时器文档:

preFER的ScheduledThreadPoolExecutor新的code ......此类不提供有关任务调度的实时性的保证。

Prefer ScheduledThreadPoolExecutor for new code...This class does not offer guarantees about the real-time nature of task scheduling.

如果需要重新启动,以获取应用程序的工作,它可能是与定时器10秒后不触发。需要注意的是,这并不一定意味着GPS本身不工作。

If a reboot is required to get the app working, its likely something to do with the Timer not firing after 10 seconds. Note that this doesn't necessarily mean that GPS itself isn't working.

处理程序应该做的工作,它的设计为Android,所以我建议使用它。它看起来像:

Handler should do the job and it's designed for Android, so I'd suggest using it. It looks like:

Handler handler = new Handler();
handler.postDelayed(getLastLocation, 10000);

...和你的 GetLastLocation 将改变为:

Runnable getLastLocation = new Runnable() {
    @Override
    public void run() {
        ...
    }
}

...和你的取消()等方法需要引用的处理程序。

...and your cancel() and other methods would need to reference the Handler.

另外请注意,你声明的对象定位在MainActivity与NETWORK_PROVIDER提供商类型,然后设置纬度/经度该对象。

Also, note that you're declaring the Location object in your MainActivity with a provider type of NETWORK_PROVIDER, and then setting the lat/long in that object.

public Location mUserCoordinates = new Location(LocationManager.NETWORK_PROVIDER);

所以,在MainActivity位置类型将始终显示为 NETWORK_PROVIDER ,无论实际的源。

另外,并不像你MainActivity需要实现LocationListener的,因为它从来没有与LocationManager注册。

Also, doesn't look like your MainActivity needs to implement LocationListener, as its never registered with the LocationManager.

最后,而不是使用两个监听GPS和网络,我建议使用在谷歌融合的位置提供播放服务,为这里讨论: http://developer.android.com/training/location/receive-location -updates.html

Finally, instead of using two listeners for GPS and NETWORK, I would suggest using the Fused location provider in Google Play Services, as discussed here: http://developer.android.com/training/location/receive-location-updates.html

您将被限制在设备Android 2.2及更高版本与谷歌播放安装服务,但在我看来,它的价值,以避免某些位置的偏心的经营平台和管理多个供应商。欲了解更多有关融合的位置信息提供,以及如何它不同于直接聆听全球定位系统和网络提供商,请参阅本 2013谷歌I / O presentation - 超越蓝点:在Android中位置的新功能

You'll be limited to devices Android 2.2 and up with Google Play Services installed, but in my opinion its worth it to avoid dealing with some of the eccentricities of location in the platform and managing more than one provider. For more about Fused location provider and how it differs from listening directly to GPS and NETWORK providers, see this 2013 Google I/O presentation - Beyond the Blue Dot: New Features in Android Location

这篇关于Android的GPS不工作,直到重新启动新手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆