gpt4 book ai didi

Android 无法解析方法 requestLocationUpdates FusedLocationProviderAPI

转载 作者:IT老高 更新时间:2023-10-28 23:36:56 26 4
gpt4 key购买 nike

我正在尝试关注 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();
}

}

最佳答案

LocationServices.FusedLocationApi.requestLocationUpdates 的 locationListener

必须使用 LocationListener

不是

android.location.LocationListener

但是

com.google.android.gms.location.LocationListener

关于Android 无法解析方法 requestLocationUpdates FusedLocationProviderAPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28954421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com