位置监听器无法到达位置

时间:2014-11-25 00:28:09

标签: android fragment

我的应用使用用户位置并有片段。在主要活动中,这些片段会发生

但这里有一个问题。我实施了#34;位置监听器"接口到我的片段类,并将断点放在" onLocationChanged"事件。程序永远不会到达断点。

为什么我无法访问用户位置?

这是我的代码:

public class NearestCoffeeVenueFragment extends Fragment implements LocationListener{
    // GPS Variables
    private LocationManager locationManager;
    private Location lastLocation;
    private  String provider;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,this);

        provider = LocationManager.NETWORK_PROVIDER;
        Location location = locationManager.getLastKnownLocation(provider);
        if(location != null){
            lastLocation = location;
            Toast.makeText(getActivity(), getString(R.string.gps_success), Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(getActivity(),getString(R.string.gps_fail),Toast.LENGTH_SHORT).show();
            cannotReachGpsWarning();
        }


        return inflater.inflate(R.layout.fragment_nearest_coffee_venue, container, false);
    }

 /** LocationListener Interface Functions
     * */
    @Override
    public void onLocationChanged(Location location){
        Toast.makeText(getActivity(),"Long: "+location.getLongitude()+" Lat:"+location.getLatitude(),Toast.LENGTH_LONG).show();
        lastLocation = location;
    }
    @Override
    public void onStatusChanged(String provider,int status,Bundle extras){

    }
    @Override
    public void onProviderEnabled(String provider){
        Toast.makeText(getActivity(),getString(R.string.gps_enabled_provider)+provider,Toast.LENGTH_LONG).show();
    }
    @Override
    public  void onProviderDisabled(String provider){
        Toast.makeText(getActivity(),getString(R.string.gps_disabled_provider)+provider,Toast.LENGTH_LONG).show();
    }
        /// warning messages and buttons setted from strings file.
    private void cannotReachGpsWarning(){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(getString(R.string.gps_disabled))
                .setCancelable(false)
                .setPositiveButton(getString(R.string.gps_enable),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                showGPSOptions();
                            }
                        });
        builder.setNegativeButton(getString(R.string.gps_disable),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();

    }

    private void showGPSOptions(){
        Intent gpsOptionsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(gpsOptionsIntent);
    }

正如我所说,我没有看到任何祝酒词或其他任何东西。片段没有在" onLocationChanged"上点击断点。功能。这意味着片段无法到达位置。片段开始时,我看到"无法到达位置"即使手机的位置已开启也会出错。

1 个答案:

答案 0 :(得分:0)

您是否在设备中为两个来源GPS和网络启用了位置?目前在您的代码中您要求网络提供商,如果禁用此提供程序,方法getLastKnownLocation将返回null。