Android经度和纬度,未知的ACCESS_FINE_LOCATION权限错误

时间:2015-02-28 11:02:48

标签: android locationmanager

我正在努力解决Android上长期存在的纬度和经度问题。

这是我的活动:

public class MainActivity extends ActionBarActivity {


General general;

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

    setTitle(ConfigApp.appName+ ConfigApp.appVersion);

    general = new General();
    general.getGeo().setLocationManager((LocationManager) getSystemService(Context.LOCATION_SERVICE));
    Location lastLocation = general.getGeo().getLocationManager().getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if(lastLocation != null){
        general.getGeo().updateLocation(lastLocation);
    }


    System.out.println(general.getGeo().getLat());
    System.out.println(general.getGeo().getLon());

}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    general.getGeo().getLocationManager().requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, general.getGeo().getLocationListener());
    general.getGeo().getLocationManager().requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, general.getGeo().getLocationListener());
}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    general.getGeo().getLocationManager().removeUpdates(general.getGeo().getLocationListener());
}}

这是我的AndroidManifest里面的清单标签:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

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

class General包含Geo类:

public class General {


private Geo geo;


public General(){
    this.geo = new Geo();
}

public Geo getGeo() {
    return geo;
}

public void setGeo(Geo geo) {
    this.geo = geo;
}}

和Geo类是这样的:

public class Geo {

private double lat;
private double lon;
private LocationManager locationManager;
private LocationListener locationListener;



public Geo(){

    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            updateLocation(location);
        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };
}

public void updateLocation(Location location){
    this.lat = location.getLatitude();
    this.lon = location.getLongitude();
}

public double getLat() {
    return lat;
}

public void setLat(double lat) {
    this.lat = lat;
}

public double getLon() {
    return lon;
}

public void setLon(double lon) {
    this.lon = lon;
}

public LocationManager getLocationManager() {
    return locationManager;
}

public void setLocationManager(LocationManager locationManager) {
    this.locationManager = locationManager;
}

public LocationListener getLocationListener() {
    return locationListener;
}

public void setLocationListener(LocationListener locationListener) {
    this.locationListener = locationListener;
} }

我总是收到这个错误:

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

1 个答案:

答案 0 :(得分:3)

您必须在AndroidManifest文件中添加以下权限:

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