Android 使用 GoogleApiClient + LocationServices 未更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25047910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:02:32  来源:igfitidea点击:

Using GoogleApiClient + LocationServices not updating

androidlocationgoogle-play-servicesandroid-locationgoogle-api-client

提问by TomDT

I'm just trying to do a simple 'tutorial' app to get my phone's location (to learn how to use it later in some other app) but I'm just not getting anywhere.

我只是想做一个简单的“教程”应用程序来获取我手机的位置(以了解稍后如何在其他应用程序中使用它),但我只是无处可去。

What I've done

我所做的

  1. Android Developer's tutorial :First of, I followed the tutorial in the Android's developer site (developer.android.com/training/location/receive-location-updates.html).

    Just like indicated in there, I used a Location, LocationClient and LocationRequest, initializing them (and setting them up) in onCreate. LocationClient is connected and disconnected in onStart and onStop.

    I'm requesting location update after I'm connected (in onConnected). I verify that GooglePlayServices are available before making this call and I'm still not getting any update in "onLocationChanged".

  2. GoogleApiClient :I noticed that LocationClient is deprecated and LocationServices are preferred (developer.android.com/reference/com/google/android/gms/location/LocationClient.html).

    As indicated here : https://developer.android.com/reference/com/google/android/gms/location/FusedLocationProviderApi.html, I use a GoogleApiClient and set it to LocationServices (also https://stackoverflow.com/a/25013850/3802589). So then I set it all to use this but still I'm not getting any updates on the location.

  1. Android 开发者教程:首先,我遵循了 Android 开发者站点 (developer.android.com/training/location/receive-location-updates.html) 中的教程。

    就像那里指出的那样,我使用了 Location、LocationClient 和 LocationRequest,在 onCreate 中初始化它们(并设置它们)。LocationClient 在 onStart 和 onStop 中连接和断开。

    我在连接后请求位置更新(在 onConnected 中)。在进行此调用之前,我验证了 GooglePlayServices 是否可用,但我仍然没有在“onLocationChanged”中获得任何更新。

  2. GoogleApiClient :我注意到 LocationClient 已被弃用,而 LocationServices 是首选(developer.android.com/reference/com/google/android/gms/location/LocationClient.html)。

    如此处所示:https://developer.android.com/reference/com/google/android/gms/location/FusedLocationProviderApi.html,我使用 GoogleApiClient 并将其设置为 LocationServices (也https://stackoverflow.com/a /25013850/3802589)。然后我将其全部设置为使用它,但仍然没有收到有关该位置的任何更新。

onLocationChanged it should print if there's a response. I also put a button that prints location or 'nothing'.

onLocationChanged 如果有响应,它应该打印。我还放了一个按钮来打印位置或“无”。

Remarks

评论

I checked using Google Maps to see if maybe I had something off but it's working just fine.

我用谷歌地图检查过,看看我是否有什么问题,但它工作得很好。

Project

项目

build.gradle

构建.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:5.0.77'
    compile "com.android.support:support-v4:20.0.+"
}

Manifest

显现

...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
...
<meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
...

Code

代码

public class MyActivity extends Activity  implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {

    private GoogleApiClient mLocationClient;
    private Location mCurrentLocation;
    LocationRequest mLocationRequest;

    ...
    /* Constant fields - request interval */
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        mLocationClient = new GoogleApiClient.Builder(getApplicationContext())
                                .addApi(LocationServices.API)
                                .addConnectionCallbacks(this)
                                .addOnConnectionFailedListener(this)
                                 .build();

        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

    }

    @Override
    protected void onStart() {
        super.onStart();
        mLocationClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();

        LocationServices.FusedLocationApi.removeLocationUpdates(mLocationClient, this);

        mLocationClient.disconnect();
    }

    public void onClick(View view) {

        if (mCurrentLocation != null) {
            Log.i("Location", mCurrentLocation.toString());
        } else {
            Log.i("Location", "nothing");
        }

    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d("Location Update", "CHANGED");
        mCurrentLocation = location;
    }

    @Override
    public void onConnected(Bundle bundle) {
        // Display the connection status
        Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
        if(servicesConnected()) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient, mLocationRequest, this);
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(this, "Disconnected. Please re-connect.",
                Toast.LENGTH_SHORT).show();
    }

logcat

日志猫

8948-8948/example.android.com.test D/Location Updates﹕ Google Play services is available.
8948-8948/example.android.com.test I/Location﹕ nothing

I feel that I might be forgetting something really simple... but I just can't see it and I've spent too much time trying to get just a simple location...

我觉得我可能忘记了一些非常简单的东西......但我就是看不到它,我花了太多时间试图找到一个简单的位置......

I could maybe just use the android.location API but... It bothers me that this is so hard (supposedly being easier).

我也许可以只使用 android.location API 但是...这让我感到困扰,这太难了(据说更容易)。

采纳答案by TomDT

The mistake was only that wifi was not on... And I didn't realise.

错误只是 wifi 没有打开......我没有意识到。

As to what GregM said, if I'm correct that's the old API (most of it deprecated right now) and as stated in the developer's site (https://developer.android.com/google/auth/api-client.html) you have to use a GoogleApiClient as shown in my original question (and same for the callbacks).

至于 GregM 所说的,如果我是对的,那是旧的 API(现在大部分已弃用),如开发人员网站 ( https://developer.android.com/google/auth/api-client.html) 中所述) 您必须使用 GoogleApiClient,如我的原始问题所示(对于回调也是如此)。

回答by GregM

The three callbacks:

三个回调:

public void onConnectionFailed(ConnectionResult arg0){}
public void onConnected(Bundle bundle){}
public void onConnectionSuspended(int arg0){}

must be implemented. However, I don't see onConnectionFailedbeing overridden. The compiler should have given an error in this case. There are two LocationListenerimplementations one for LocationListenerand the other gms.location.LocationListener. You may want to check if you are using the gms version.

必须实施。但是,我没有看到onConnectionFailed被覆盖。在这种情况下,编译器应该给出错误。有两种LocationListener实现,一种是 for LocationListener,另一种是gms.location.LocationListener。您可能需要检查您是否使用的是 gms 版本。

I hope Google will update their training sample code to the new client model and eliminate further confusion.

我希望谷歌将他们的训练示例代码更新到新的客户端模型并消除进一步的混乱。