Android位置权限错误
我正在尝试使用谷歌地图API。我已将清单文件中的所有必要权限设置为:Android位置权限错误,android,permissions,location,maps,parse-server,Android,Permissions,Location,Maps,Parse Server,我正在尝试使用谷歌地图API。我已将清单文件中的所有必要权限设置为: <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERN
<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" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
它显示了由权限检查代码(如下)生成的错误。我也不知道该进入街区什么地方;条件语句似乎已经在进行必要的检查
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
我的代码:
package com.project.korsa.korsa;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.parse.FindCallback;
import com.parse.ParseACL;
import com.parse.ParseException;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import java.util.List;
public class YourLocation extends FragmentActivity implements OnMapReadyCallback, LocationListener {
private GoogleMap mMap;
LocationManager locationManager;
String provider;
TextView infoTextView;
Button requestKorsaButton;
Boolean requestActive = false;
public void requestKorsa(View view) {
if (requestActive == false) {
Log.i("MyApp", "Korsa requesed");
ParseObject request = new ParseObject("Requests");
request.put("requesterUsername", ParseUser.getCurrentUser().getUsername());
ParseACL parseACL = new ParseACL();
parseACL.setPublicWriteAccess(true);
parseACL.setPublicReadAccess(true);
request.setACL(parseACL);
request.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
infoTextView.setText("Finding Korsa driver...");
requestKorsaButton.setText("Cancel Korsa");
requestActive = true;
}
else infoTextView.setText("Error...");
}
});
} else {
infoTextView.setText("Korsa Cancelled.");
requestKorsaButton.setText("Request Korsa");
requestActive = false;
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Requests");
query.whereEqualTo("requesterUsername", ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
for (ParseObject object : objects) {
object.deleteInBackground();
}
}
}
}
});
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_location);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
infoTextView = (TextView) findViewById(R.id.infoTextView);
requestKorsaButton = (Button) findViewById(R.id.requestKorsa);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(provider, 400, 1, this);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
updateLocation(location);
}
}
public void updateLocation(Location location) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10));
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Your Location"));
if (requestActive == true) {
final ParseGeoPoint userLocation = new ParseGeoPoint(location.getLatitude(), location.getLongitude());
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Requests");
query.whereEqualTo("requesterUsername", ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
for (ParseObject object : objects) {
object.put("requesterLocation", userLocation);
object.saveInBackground();
}
}
}
}
});
}
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
/*if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
mMap.setMyLocationEnabled(true);*/
}
@Override
public void onLocationChanged(Location location) {
mMap.clear();
updateLocation(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
package com.project.korsa.korsa;
导入android.content.Context;
导入android.content.pm.PackageManager;
导入android.location.Criteria;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.support.v4.app.ActivityCompat;
导入android.support.v4.app.FragmentActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.OnMapReadyCallback;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.MarkerOptions;
导入com.parse.FindCallback;
导入com.parse.ParseACL;
导入com.parse.ParseException;
导入com.parse.ParseGeoPoint;
导入com.parse.ParseObject;
导入com.parse.ParseQuery;
导入com.parse.ParseUser;
导入com.parse.SaveCallback;
导入java.util.List;
公共类YourLocation扩展了FragmentActivity在MapReadyCallback、LocationListener上的实现{
私有谷歌地图;
地点经理地点经理;
字符串提供者;
文本视图信息文本视图;
按钮请求Korsabutton;
布尔requestActive=false;
公共void requestKorsa(视图){
if(requestActive==false){
Log.i(“MyApp”、“Korsa requesed”);
ParseObject请求=新的ParseObject(“请求”);
put(“requesterUsername”,ParseUser.getCurrentUser().getUsername());
ParseACL ParseACL=新的ParseACL();
parseACL.setPublicWriteAccess(true);
parseACL.setPublicReadAccess(true);
setACL(parseACL);
request.saveInBackground(新的SaveCallback(){
@凌驾
公共作废完成(Parsee异常){
如果(e==null){
infoTextView.setText(“查找Korsa驱动程序…”);
requestKorsaButton.setText(“取消Korsa”);
requestActive=true;
}
else infoTextView.setText(“错误…”);
}
});
}否则{
infoTextView.setText(“Korsa已取消”);
requestKorsaButton.setText(“requestkorsa”);
requestActive=false;
ParseQuery查询=新的ParseQuery(“请求”);
query.whereEqualTo(“requesterUsername”,ParseUser.getCurrentUser().getUsername());
findInBackground(新的FindCallback(){
@凌驾
公共void done(列出对象,parsee异常){
如果(e==null){
如果(objects.size()>0){
for(ParseObject对象:对象){
object.deleteInBackground();
}
}
}
}
});
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u您的位置);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
infoTextView=(TextView)findViewById(R.id.infoTextView);
requestKorsaButton=(按钮)findViewById(R.id.requestKorsa);
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
provider=locationManager.getBestProvider(新标准(),false);
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
考虑到呼叫
//ActivityCompat#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//int[]格兰特结果)
//处理用户授予权限的情况。请参阅文档
//对于ActivityCompat,请请求权限以获取更多详细信息。
返回;
}
locationManager.requestLocationUpdates(提供者,400,1,this);
Location Location=locationManager.getLastKnownLocation(提供者);
如果(位置!=null){
更新位置(位置);
}
}
public void updateLocation(位置){
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(newlatlng(location.getLatitude(),location.getLongitude()),10));
mMap.addMarker(新的MarkerOptions().position(新的LatLng(location.getLatitude(),location.getLongitude()).title(“您的位置”);
if(requestActive==true){
final ParseGeoPoint userLocation=新的ParseGeoPoint(location.getLatitude(),location.getLength());
ParseQuery查询=新的ParseQuery(“请求”);
query.whereEqualTo(“requesterUsername”,ParseUser.getCurrentUser().getUsername());
findInBackground(新的FindCallback(){
@凌驾
已完成公开作废(Lis)
package com.project.korsa.korsa;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.parse.FindCallback;
import com.parse.ParseACL;
import com.parse.ParseException;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import java.util.List;
public class YourLocation extends FragmentActivity implements OnMapReadyCallback, LocationListener {
private GoogleMap mMap;
LocationManager locationManager;
String provider;
TextView infoTextView;
Button requestKorsaButton;
Boolean requestActive = false;
public void requestKorsa(View view) {
if (requestActive == false) {
Log.i("MyApp", "Korsa requesed");
ParseObject request = new ParseObject("Requests");
request.put("requesterUsername", ParseUser.getCurrentUser().getUsername());
ParseACL parseACL = new ParseACL();
parseACL.setPublicWriteAccess(true);
parseACL.setPublicReadAccess(true);
request.setACL(parseACL);
request.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
infoTextView.setText("Finding Korsa driver...");
requestKorsaButton.setText("Cancel Korsa");
requestActive = true;
}
else infoTextView.setText("Error...");
}
});
} else {
infoTextView.setText("Korsa Cancelled.");
requestKorsaButton.setText("Request Korsa");
requestActive = false;
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Requests");
query.whereEqualTo("requesterUsername", ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
for (ParseObject object : objects) {
object.deleteInBackground();
}
}
}
}
});
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_location);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
infoTextView = (TextView) findViewById(R.id.infoTextView);
requestKorsaButton = (Button) findViewById(R.id.requestKorsa);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(provider, 400, 1, this);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
updateLocation(location);
}
}
public void updateLocation(Location location) {
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 10));
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Your Location"));
if (requestActive == true) {
final ParseGeoPoint userLocation = new ParseGeoPoint(location.getLatitude(), location.getLongitude());
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Requests");
query.whereEqualTo("requesterUsername", ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
if (objects.size() > 0) {
for (ParseObject object : objects) {
object.put("requesterLocation", userLocation);
object.saveInBackground();
}
}
}
}
});
}
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
/*if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
mMap.setMyLocationEnabled(true);*/
}
@Override
public void onLocationChanged(Location location) {
mMap.clear();
updateLocation(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
Manifest.permission.ACCESS_FINE_LOCATION and
Manifest.permission.ACCESS_COARSE_LOCATION
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_LOCATION_REQUEST_CODE);
public static boolean checkPermission(final Context context) {
return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
private void showPermissionDialog() {
if (!LocationController.checkPermission(this)) {
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_LOCATION_REQUEST_CODE);
}
}
public void onClick(View v) {
int id = v.getId();
if (id == R.id.floating_button) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Mapview mapview = new Mapview();
fragmentTransaction.replace(R.id.mframe_mapview, mapview).addToBackStack(null);
fragmentTransaction.commit();
alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
launchTwitter(revealView);
}
}
private GoogleMap mgoogleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mapview, container, false);
return v;
}
@Override
public void onViewCreated(View view,Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapView mMapView = (MapView)view.findViewById(R.id.map_view);
if(mMapView != null)
{
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mgoogleMap = googleMap;
mgoogleMap.addMarker(new MarkerOptions().position(new LatLng(-34, 151)).title("Marker in Sydney").snippet("i hope to"));
CameraPosition cameraPosition = CameraPosition.builder().target(new LatLng(-34, 151)).zoom(16).bearing(0).tilt(45).build();
mgoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
uses-permission android:name="android.permission.INTERNET" />