Android无法解析方法requestLocationUpdates FusedLocationProviderAPI

时间:2015-03-10 00:52:39

标签: android fusedlocationproviderapi

我试图按照Android Receiving Location Updates教程进行操作。示例代码为here on Github

除了这一行,一切正常:         LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

它引发cannot resolve method requestLocationUpdates错误。我在documentation中看不到任何内容,但我对Android很新,所以可能会遗漏一些内容:)。

这里是完整的代码。我出错的任何想法?谢谢!

package location.test.example.com;

import android.app.Fragment;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

import java.util.ArrayList;
import java.util.Date;

public class LocationFragment extends Fragment implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

    protected GoogleApiClient mGoogleApiClient;
    protected Location mCurrentLocation;
    protected LocationRequest mLocationRequest;
    protected String mLastUpdateTime;

    public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 5000;

    public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 5;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLastUpdateTime = "";

        buildGoogleApiClient();

        if (NavUtils.getParentActivityName(getActivity()) != null) {
            getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
        }
        setHasOptionsMenu(true);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_location, container, false);    

        return v;
    }

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        createLocationRequest();
    }

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();

        mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);

        mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);

        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        startLocationUpdates();
    }

    protected void startLocationUpdates() {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onLocationChanged(Location location) {
        mCurrentLocation = location;
        mLastUpdateTime = DateFormat.getDateFormat(getActivity()).format(new Date());
        Toast.makeText(getActivity(), location.toString() + "", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnected(Bundle connectionHint) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.i("LocationFragment", "Connection failed: ConnectionResult.getErrorCode() " + result.getErrorCode());
    }

    @Override
    public void onConnectionSuspended(int cause) {
        Log.i("LocationFragment", "Connection suspended");
        mGoogleApiClient.connect();
    }

}

6 个答案:

答案 0 :(得分:71)

您必须LocationListener使用LocationServices.FusedLocationApi.requestLocationUpdates's locationListener

  

android.location.LocationListener

但是

  

<强> com.google.android.gms.location.LocationListener

答案 1 :(得分:6)

导入

import com.google.android.gms.location.LocationListener;

而不是

import android.location.LocationListener;

答案 2 :(得分:4)

替换

import android.location.LocationListener;

来自

import com.google.android.gms.location.LocationListener;

和 删除这些覆盖方法

public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}

答案 3 :(得分:0)

导入android.location.LocationListenercom.google.android.gms.location.LocationListener无用,因为程序包仍未使用。 实现com.google.android.gms.location.LocationListener接口即可。

答案 4 :(得分:0)

而不是更改import语句,  使用  实施com.google.android.gms.location.LocationListener

而不仅仅是LocationListener / android.location.LocationListener。

它确实解决了我的问题。

答案 5 :(得分:0)

需要更新版本的google play services 10.0.0 +

compile 'com.google.android.gms:play-services:10.0.0'