Java Android位置侦听器未启动

Java Android位置侦听器未启动,java,android,android-fragments,location,Java,Android,Android Fragments,Location,我试图在我的android应用程序中检测用户当前位置的变化。但是,不会触发onLocationChanged。顺便说一句,我试图在一个片段中实现这一点 片段: public class Fragment_Map extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { V

我试图在我的android应用程序中检测用户当前位置的变化。但是,不会触发onLocationChanged。顺便说一句,我试图在一个片段中实现这一点

片段:

public class Fragment_Map extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_fragment__live_map, container, false);
        rootView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {

                    return true;
                }
                return false;
            }
        });

        // The minimum time (in miliseconds) the system will wait until checking if the location changed
        int minTime = 1000;
        // The minimum distance (in meters) traveled until you will be notified
        float minDistance = 1;
        // Create a new instance of the location listener
        MyLocationListener myLocListener = new MyLocationListener();
        // Get the location manager from the system
        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        // Get the criteria you would like to use
        Criteria criteria = new Criteria();
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setSpeedRequired(false);
        // Get the best provider from the criteria specified, and false to say it can turn the provider on if it isn't already
        String bestProvider = locationManager.getBestProvider(criteria, false);
        // Request location updates
        locationManager.requestLocationUpdates(bestProvider, minTime, minDistance, myLocListener);

        return rootView;
}

private class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location loc) {
            if (loc != null) {
                Log.d("CHECKLOCA", "fired");
                Toast.makeText(getActivity(), "fired", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onProviderDisabled(String arg0) {
            // Do something here if you would like to know when the provider is disabled by the user
        }

        @Override
        public void onProviderEnabled(String arg0) {
            // Do something here if you would like to know when the provider is enabled by the user
        }

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // Do something here if you would like to know when the provider status changes
        }
    }

为什么位置没有改变?是因为它是个碎片吗?在黑客的中间,你能帮我解决这个讨厌的bug吗?

< P>也许你的设备不能提供U设置来查找位置的标准。 可以在onCreateView()中使用此代码:


可能您的设备无法提供您设置的查找位置的标准。。 可以在onCreateView()中使用此代码:


您可以使用
GoogleApiClient
请求位置,并将优先级设置为
priority\u HIGH\u准确性
。您可以创建
片段
来实现
GoogleApicClient
回调
——然后实例化并连接
GoogleApicClient
,如下所示:

googleApiClient = new GoogleApiClient.Builder(getActivity())
                                .addApi(LocationServices.API)
                                .addConnectionCallbacks(this)
                                .addOnConnectionFailedListener(this)
                                .build();
      googleApiClient.connect();
public interface OnLocationReceived 
{
 public void onLocationReceived(LatLng latlong);
 public void onLocationReceived(Location location);
 public void onConntected(Bundle bundle);
 public void onConntected(Location location);
}
然后,一旦您的客户端连接到Location Services API,您就可以请求更新:

    public void onConnected(Bundle bundle){
    ...
    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(180000); // update interval - eg (3) minutes 
                LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, myLocListener);

    ...

}

我希望这有帮助

您可以使用
GoogleAppClient
请求位置并将优先级设置为
priority\u HIGH\u accurity
。您可以创建
片段
来实现
GoogleApicClient
回调
——然后实例化并连接
GoogleApicClient
,如下所示:

googleApiClient = new GoogleApiClient.Builder(getActivity())
                                .addApi(LocationServices.API)
                                .addConnectionCallbacks(this)
                                .addOnConnectionFailedListener(this)
                                .build();
      googleApiClient.connect();
public interface OnLocationReceived 
{
 public void onLocationReceived(LatLng latlong);
 public void onLocationReceived(Location location);
 public void onConntected(Bundle bundle);
 public void onConntected(Location location);
}
然后,一旦您的客户端连接到Location Services API,您就可以请求更新:

    public void onConnected(Bundle bundle){
    ...
    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(180000); // update interval - eg (3) minutes 
                LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, myLocListener);

    ...

}
我希望这有帮助

  • 创建如下界面:

    googleApiClient = new GoogleApiClient.Builder(getActivity())
                                    .addApi(LocationServices.API)
                                    .addConnectionCallbacks(this)
                                    .addOnConnectionFailedListener(this)
                                    .build();
          googleApiClient.connect();
    
    public interface OnLocationReceived 
    {
     public void onLocationReceived(LatLng latlong);
     public void onLocationReceived(Location location);
     public void onConntected(Bundle bundle);
     public void onConntected(Location location);
    }
    
    2.将其放入
    MyLocationListener

    @Override
     public void onLocationChanged(Location location) 
    {
      if (!isLocationReceived) {
        mLocationReceived.onConntected(location);
        isLocationReceived = true;
       }
       if (mLocationReceived != null) {
        mLocationReceived.onLocationReceived(location);
       }
       latLong = getLatLng(location);
       if (mLocationReceived != null && latLong != null) {
        mLocationReceived.onLocationReceived(latLong);
       }
    }
    
  • 创建如下界面:

    googleApiClient = new GoogleApiClient.Builder(getActivity())
                                    .addApi(LocationServices.API)
                                    .addConnectionCallbacks(this)
                                    .addOnConnectionFailedListener(this)
                                    .build();
          googleApiClient.connect();
    
    public interface OnLocationReceived 
    {
     public void onLocationReceived(LatLng latlong);
     public void onLocationReceived(Location location);
     public void onConntected(Bundle bundle);
     public void onConntected(Location location);
    }
    
    2.将其放入
    MyLocationListener

    @Override
     public void onLocationChanged(Location location) 
    {
      if (!isLocationReceived) {
        mLocationReceived.onConntected(location);
        isLocationReceived = true;
       }
       if (mLocationReceived != null) {
        mLocationReceived.onLocationReceived(location);
       }
       latLong = getLatLng(location);
       if (mLocationReceived != null && latLong != null) {
        mLocationReceived.onLocationReceived(latLong);
       }
    }
    

  • 更改此关键字。。对于MyLocationListener来说,这实际上不是很好的代码,问题将在中讨论。但是你的标准可能是对的。更改此关键字。。对于MyLocationListener来说,这实际上不是很好的代码,问题将在中讨论。但你的标准可能是对的。