Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.SecurityException "gps" location provider requires ACCESS_FINE_LOCATION permission

Tags:

android

So this is my MyLocationListener Class

package com.example.gpslocater;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MyLocationListener implements LocationListener {

        public TextView mLocationTextView;
    Context mContent;
    public MyLocationListener(Context context) {
        mContent = context;
    }
    @Override
    public void onLocationChanged(Location location) {

        location.getLatitude();
        location.getLongitude();

        String Text = "My current location is :" + 
        "Latitude = " + location.getLatitude() +
        " Longitude = " + location.getLongitude();
        //figure out a way to make it display through a text view
        mLocationTextView.setText(Text);
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(mContent, "Gps Disabled", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(mContent, "Gps Enabled", Toast.LENGTH_SHORT);


    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}

And this is my GPS Activity class

public class GPSActivity extends Activity {
    private Button mGPSButton;
    private TextView mLatitudeTextView;
    private TextView mLongitudeTextView;
    private LocationManager mLocationManager;
    private LocationListener mLocationListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gps);

        mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mLocationListener = new MyLocationListener(null);
        mGPSButton = (Button)findViewById(R.id.press_button);
        mGPSButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.g, menu);
        return true;
    }

}

Now when I run it it works but after I press the button the program stops responding there are a lot of error messages a busy cat

There is an error message that says java.lang.SecurityException "gps" location provider requires ACCESS_FINE_LOCATION permission, do I have to add that if so where do I add it? I think that this was the cause of my program to crash.

like image 882
john parker Avatar asked May 22 '13 12:05

john parker


2 Answers

Make sense from logcat provided. Add this in your manifest

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
like image 149
CRUSADER Avatar answered Oct 19 '22 01:10

CRUSADER


You have to add the following permissions to the Android manifest:

AndroidManifest.xml

...

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

...

You can also use the Permission Editor of Eclipse.

like image 34
JimTim Avatar answered Oct 19 '22 00:10

JimTim