【问题标题】:Android ACCESS_FINE_LOCATION permission not being granted or something?未授予 Android ACCESS_FINE_LOCATION 权限或其他什么?
【发布时间】:2015-06-26 11:23:01
【问题描述】:

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>

【问题讨论】:

  • 让我们知道 manifest snnippet.. 请添加 manifest sn-p 以解决错误
  • 添加了清单。请检查:)
  • 老兄把所有权限都放在application标签之外
  • getLastKnownLocation 对于跟踪用户位置不准确也不正确,如果您移动也不会更新
  • 检查这个 - stackoverflow.com/a/38608800/2806800 我已经回答了这个问题

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


【解决方案1】:

两个问题:

  1. 您的所有&lt;uses-permission&gt;&lt;permission&gt;&lt;uses-feature&gt; 元素都需要移动到&lt;application&gt;外部。它们应该是根 &lt;manifest&gt; 元素的直接子元素。

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

【讨论】:

  • 它对我有用,但现在它显示地图上的蓝色圆圈而不是标记。
【解决方案2】:

把所有在sn-p下面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" />

【讨论】:

    猜你喜欢
    • 2011-10-03
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 2011-05-31
    相关资源
    最近更新 更多