0

I think there is a problem in My Location Listener but I am not able to pinpoint it. When I remove the code to get GPS my application is working fine.

public class RescueFragment extends Fragment{

    public RescueFragment(){}
    private static final long mindisch=1;
    private static final long mintim=1000;
    protected LocationManager locationManager;
    protected Double latitude,longitude;

    TextView alert_display;
    Button btn;

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

        View rootView = inflater.inflate(R.layout.fragment_rescue, container, false);
        alert_display = (TextView)rootView.findViewById(R.id.alert_display);       
        btn = (Button) rootView.findViewById(R.id.button_rescue_me);

        locationManager =(LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,mindisch,mintim, new MyLocationListener()   );

        btn.setOnClickListener(new OnClickListener() {  

            @Override
            public void onClick(View arg0) {


                alert_display.setText("Alert Sent to User");
                showCurrentLocation();
            }
        });

        return rootView;
    }

    protected void showCurrentLocation()
    {
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location!=null)
        {
            latitude=location.getLatitude();
            longitude=location.getLongitude();
            Toast.makeText(getActivity(), "Alert sent. Location: "+ latitude+" "+ longitude, Toast.LENGTH_LONG).show();
        }
    }

    private class MyLocationListener implements LocationListener{

        @Override
        public void onLocationChanged(Location location) {
            latitude=location.getLatitude();
            longitude=location.getLongitude();
            Toast.makeText(getActivity(), "Alert sent. Location: "+ latitude+" "+ longitude, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Toast.makeText(getActivity(), "Provider Status Changed: ", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(getActivity(), "GPS ON", Toast.LENGTH_LONG).show();          
        }

        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(getActivity(), "GPS OFF:", Toast.LENGTH_LONG).show();            
        }
    }
}
Simon W
  • 5,481
  • 3
  • 24
  • 35
naveen
  • 5
  • 2
  • 3
    kindly post the error log . – n j May 02 '16 at 12:06
  • did you add the permission to fetch location in your manifest file?? – himanshu1496 May 02 '16 at 12:08
  • yes i did add permission in mainfest – naveen May 02 '16 at 12:14
  • 05-02 13:07:29.348: E/AndroidRuntime(15930): FATAL EXCEPTION: main 05-02 13:07:29.348: E/AndroidRuntime(15930): Process: info.androidhive.slidingmenu, PID: 15930 05-02 13:07:29.348: E/AndroidRuntime(15930): java.lang.RuntimeException: Unable to start activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.MainActivity}: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission. 05-02 13:07:29.348: E/AndroidRuntime(15930): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2484) – naveen May 02 '16 at 12:15
  • This is permission exception – Yasir Tahir May 02 '16 at 12:16

1 Answers1

1

Add this Permissions in your Manifest.

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

For Android Versions 6.0 you need to allow user's to give Runtime Permissions.

if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
        PackageManager.PERMISSION_GRANTED &&
        ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
        PackageManager.PERMISSION_GRANTED) {
    googleMap.setMyLocationEnabled(true);
    googleMap.getUiSettings().setMyLocationButtonEnabled(true);
} else {
    Toast.makeText(this, R.string.error_permission_map, Toast.LENGTH_LONG).show();
}
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • Are you having compiled `SDK` Version 6.0 ? if Yes then you need to allow user to have `Runtime Permission` in your app. See this link http://stackoverflow.com/questions/32083913/android-gps-requires-access-fine-location-error-even-though-my-manifest-file – Jay Rathod May 02 '16 at 12:22
  • i did add it before the application tag.. should i change it position? – naveen May 02 '16 at 12:22
  • Not need to change position. Check my comment above. Are you using Android 6.0 version? – Jay Rathod May 02 '16 at 12:23