我正在努力获取一款可从手机获取GPS定位的应用。我已经为ACCESS_COURSE_LOCATION添加了XML的权限,但它仍然说我需要权限,即使权限已经在XML中。
这是我的代码:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.location.Location;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
public final class MainActivity extends AppCompatActivity {
public static final String TAG = "Kindness";
private FusedLocationProviderClient mFusedLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(
this);
mFusedLocationClient.getLastLocation().addOnSuccessListener(this,
new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
}
}
});
}
}
这是XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.byui.cit.kindness">
<uses-permission android:name="android.permisison.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/appName"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="edu.byui.cit.kindness.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
这可能是因为位置权限are considered "dangerous" permissions by Android,当您的应用程序的目标SDK为23或更高时,您需要在运行时请求“危险”权限。只是将它们添加到清单中是行不通的。
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
REQUEST_CODE);