Java 我的位置改变了吗?

Java 我的位置改变了吗?,java,android,gps,location,Java,Android,Gps,Location,我的位置记录器服务类不向我提供有关我的位置的更新信息。我换了位置有什么问题吗 public class LocationLoggerService extends Service implements LocationListener { Location location; // location String towers; // Declaring a Location Manager public LocationManager locationMan

我的位置记录器服务类不向我提供有关我的位置的更新信息。我换了位置有什么问题吗

public class LocationLoggerService extends Service implements LocationListener {

    Location location; // location
    String towers;

    // Declaring a Location Manager
    public LocationManager locationManager;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        ourLocation();
        Criteria crit = new Criteria();
        towers = locationManager.getBestProvider(crit, false);
        location = locationManager.getLastKnownLocation(towers);

        // Just af test here:
        String hej1 = Double.toString(location.getLatitude());
        String hej2 = Double.toString(location.getLongitude());
        Toast.makeText(this, "Fra Oncreate: /nLat: " + hej1 + "Long: " + hej2,
                Toast.LENGTH_LONG).show();

    }

    public void ourLocation()

    {
        // Find my current location
        locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                2000, 0, this);
    }
onLocationChanged方法不会向我提供新信息。我尝试了一些测试(toast),但它似乎没有读到这个方法

@Override
public void onLocationChanged(Location loc) {
    if (loc != null) {
        // Testing the position
        String hej3 = Double.toString(loc.getLatitude());
        String hej4 = Double.toString(loc.getLongitude());
        Toast.makeText(this, "loc: " + "Lat: " + hej3 + "Long: " + hej4,
                Toast.LENGTH_LONG).show();
    }
}

    @Override
    public void onProviderEnabled(String s) {
    }

    @Override
    public void onProviderDisabled(String s) {
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle b) {
    }

    @Override
    public void onDestroy() {

        // TODO Auto-generated method stub

        super.onDestroy();

        Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG)
                .show();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // this service will run until we stop it

        return START_STICKY;
    }

}
我的舱单

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />


<service
        android:name="LocationLoggerService"
        android:enabled="true"
        android:exported="false"
        android:label="LocationLoggerService" />
</application>

尝试按以下方式更改代码,然后重试,直接从onLocationChanged回调方法获取location对象。。。我希望您正在着手测试此功能。 位置ojbect将包含最新的纬度和经度

@Override
public void onLocationChanged(Location location) {


    // Testing onLocationChanged:
    String hej1 = Double.toString(location.getLatitude());
    String hej2 = Double.toString(location.getLongitude());
    Toast.makeText(this, "Location: "+"Lat: " + hej1 + "Long: " + hej2,
            Toast.LENGTH_LONG).show();

    String hej3 = Double.toString(loc.getLatitude());
    String hej4 = Double.toString(loc.getLongitude());
    Toast.makeText(this, "loc: " +"Lat: " + hej3 + "Long: " + hej4,
            Toast.LENGTH_LONG).show();

}

您是移动还是坐在同一位置?移动时将触发OnLocationChanged。出于测试目的,您可以在应用程序中进行一个小更改,即ocationManager.requestLocationUpdates(LocationManager.GPS\u PROVIDER,0,0,this);之后你可以恢复到2000。。。在设备中安装此技术后,步行10分钟,即可观察新位置