没有授予Android ACCESS_FINE_LOCATION权限或其他权限?

时间:2015-06-26 11:23:02

标签: android google-maps-android-api-2

AI创建了一个简单的应用程序,在用户的当前位置显示标记,并且我已使用清单中的所有权限。这是代码:

public class LocationActivity extends FragmentActivity implements LocationListener {
SupportMapFragment map;
GoogleMap googleMap;
LocationManager loc;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_location);
    map=(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    onMapReady(googleMap);
    //onLocationChanged(location);
}
public void onMapReady(GoogleMap googleMap){
    try {
        googleMap = map.getMap();
        if (googleMap != null) {
            googleMap.setMyLocationEnabled(true);
            loc = (LocationManager) getSystemService(LOCATION_SERVICE);
            Criteria crit = new Criteria();
            String best = loc.getBestProvider(crit,true);
            Location location = loc.getLastKnownLocation(best);
            LatLng myLocation = new LatLng(location.getLatitude(), location.getLongitude());
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 15));
            googleMap.addMarker(new MarkerOptions().title("You're here").position(myLocation));
        } else {
            Toast.makeText(this, "Fragment has a problem", Toast.LENGTH_SHORT).show();
        }
    }
    catch (Exception e){
        Toast.makeText(this,"we can't do it",Toast.LENGTH_SHORT).show();
    }
}

@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_location, 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
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
}

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

@Override
public void onLocationChanged(Location location) {
//something will go on here
}}

但它显示运行时错误:

  

PRIORITY_HIGH_LOCATION_REQUEST需要ACCESS_FINE_LOCATION

这里可能的原因是什么?虽然如果我删除这一行代码编译得很好 -

googleMap.setMyLocationEnabled(true);

但位置不准确 这是清单文件

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="API_Key"/>
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <permission android:name="com.example.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
    <uses-permission android:name="com.example.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <activity
        android:name=".LockScreenActivity"
        android:label="@string/title_activity_lock_screen" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".LocationActivity"
        android:label="@string/title_activity_location" >
    </activity>
</application>

2 个答案:

答案 0 :(得分:1)

将所有以下代码段放在application代码

之外
 <android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <permission android:name="com.example.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
    <uses-permission android:name="com.example.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

并在添加权限时添加您的包名称

<permission android:name="yourpackagename.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<uses-permission android:name="yourpackagename.permission.MAPS_RECEIVE" />

答案 1 :(得分:1)

两个问题:

  1. 您的所有<uses-permission><permission><uses-feature>元素都需要在<application>之外移动 。他们应该是根<manifest>元素的直接子女。

  2. 您不会像使用android:READ_PHONE_STATE那样在元素名称上使用READ_EXTERNAL_STORAGE前缀。无论如何,您都不需要READ_EXTERNAL_STORAGE,因为您要求WRITE_EXTERNAL_STORAGE