未找到requestLocationUpdates合适的方法未找到、合适、方法、requestLocationUpdates

2023-09-06 22:39:18 作者:她有公主病我又不是男仆命

这是我MainActivity.java:

This is my MainActivity.java:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

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


public class MainActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new GetCurrentLocation();
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
}

这是我GetCurrentLocation.java:

And this is my GetCurrentLocation.java:

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

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

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class GetCurrentLocation extends Activity implements LocationListener {
    String longitude;
    String latitude;
    String cityName;

    public void onLocationChanged(Location loc) {
        //editLocation.setText("");
        //pb.setVisibility(View.INVISIBLE);
        Toast.makeText(
                getBaseContext(),
                "Location changed: Lat: " + loc.getLatitude() + " Lng: "
                        + loc.getLongitude(), Toast.LENGTH_SHORT).show();
        longitude = "Longitude: " + loc.getLongitude();
        //Log.v(TAG, longitude);
        latitude = "Latitude: " + loc.getLatitude();
        //Log.v(TAG, latitude);

        /*------- To get city name from coordinates -------- */
        //String cityName = null;
        Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
        List<Address> addresses;
        try {
            addresses = gcd.getFromLocation(loc.getLatitude(),
                    loc.getLongitude(), 1);
            if (addresses.size() > 0)
                System.out.println(addresses.get(0).getLocality());
            cityName = addresses.get(0).getLocality();
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        //editLocation.setText(s);
    }

    //@Override
    public void onProviderDisabled(String provider) {}

    //@Override
    public void onProviderEnabled(String provider) {}

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


}

我得到的错误是:

The error I got is:

Error:(40, 24) error: no suitable method found for requestLocationUpdates(String,int,int,com.google.android.gms.location.LocationListener)
method LocationManager.requestLocationUpdates(long,float,Criteria,PendingIntent) is not applicable
(actual argument String cannot be converted to long by method invocation conversion)
method LocationManager.requestLocationUpdates(String,long,float,PendingIntent) is not applicable
(actual argument com.google.android.gms.location.LocationListener cannot be converted to PendingIntent by method invocation conversion)
method LocationManager.requestLocationUpdates(long,float,Criteria,android.location.LocationListener,Looper) is not applicable
(actual and formal argument lists differ in length)
method LocationManager.requestLocationUpdates(String,long,float,android.location.LocationListener,Looper) is not applicable
(actual and formal argument lists differ in length)
method LocationManager.requestLocationUpdates(String,long,float,android.location.LocationListener) is not applicable
(actual argument com.google.android.gms.location.LocationListener cannot be converted to android.location.LocationListener by method invocation conversion)

我的code我应该可以解决这部分上完全空白,我曾尝试谷歌,但没有相关的解决方案都可以找到。

I am totally blank on which part of the code I should fix and I have tried to google but no relevant solution can be found.

推荐答案

您使用了错误的 LocationListener的类。您需要替换的:

You are using the wrong LocationListener class. You need to replace this:

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

这一点:

import android.location.LocationListener;

com.google.android.gms.location 的东西是与 FusedLocationProviderApi 其用途是部分谷歌播放服务。

The com.google.android.gms.location stuff is for use with the FusedLocationProviderApi which is part of Google play services.

标准Android位置的东西是在 android.location

The standard Android location stuff is in android.location.

此外,你必须 GetCurrentLocation延伸活动。但它不是一个真正的活动。您需要删除延伸活动条款。这样会造成问题,因为你调用 getBaseContext()在类的一些方法。为了解决这个问题,有 GetCurrentLocation 的构造函数取上下文参数,并保存在一个私有成员变量。然后使用,当你需要一个上下文不是调用 getBaseContext()

Also, you have GetCurrentLocation extends Activity. But it isn't really an Activity. You need to remove the extends Activity clause. This will then cause problems because you are calling getBaseContext() in some methods of that class. To fix this, have the constructor of GetCurrentLocation take a Context parameter and save that in a private member variable. Then use that when you need a Context instead of calling getBaseContext().

MainActivity ,当你创建一个新的 GetCurrentLocation ,你可以这样做:

In MainActivity, when you create a new GetCurrentLocation, you can do this:

LocationListener locationListener = new GetCurrentLocation(this);

通过上下文参数(活动扩展上下文)。