0

我编写了这个简单的代码来获取 GPS 坐标,但它没有调用方法“onChangedLocation”。我知道只有当坐标改变时它才应该跳转到该方法中,但是一开始它总是应该跳转到那个方法中吗?

它应该在目录中显示经度和纬度。

我用我的手机进行调试。

package tabs;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;


import androfenix.whetoo.R;


public class Tab1 extends ListFragment implements LocationListener {

    double latitude;
    double longitude;


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

        String[] abc = {"Message1", "Message2", "Message3", "Message1", "Message2", "Message3", "Message1", "Message2", "Message3", "Message1", "Message2", "Message3"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, abc);
        setListAdapter(adapter);

        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
        }
        return v;
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d("onLocationChanged", "onLocationChanged wird aufgerufen");
        longitude = location.getLongitude();
        String name = String.valueOf(longitude);
        Log.d("longtitude", name);

        latitude = location.getLatitude();
        String name2 = String.valueOf(latitude);
        Log.d("latitude", name2);
    }

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

    @Override
    public void onProviderEnabled(String provider) {
    }

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

0 回答 0