Android位置权限错误
Posted
技术标签:
【中文标题】Android位置权限错误【英文标题】:Android Location Permission error 【发布时间】:2017-09-23 18:46:52 【问题描述】:我正在尝试使用谷歌地图 API。我已经在清单文件中设置了所有必要的权限:
<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" />
当我尝试使用以下代码时:
locationManager.requestLocationUpdates(provider, 400, 1, this);
Location location = locationManager.getLastKnownLocation(provider);
它显示了由权限检查代码生成的错误(如下)。而且我不知道在块中输入什么;似乎条件语句已经在进行必要的检查了。
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)
【问题讨论】:
编译时如果你不使用权限模型,你会收到警告如果目标是棉花糖及以上,请通过权限模型更好地理解developer.android.com/training/permissions/requesting.html 【参考方案1】:在代码中你必须编写代码来请求用户授予权限
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);
【讨论】:
【参考方案2】:在您正在检查权限的地方请求权限。首先检查权限
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);
【讨论】:
【参考方案3】: 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);
公共类 Mapview 扩展 Fragment 实现 OnMapReadyCallback
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" />
【讨论】:
以上是关于Android位置权限错误的主要内容,如果未能解决你的问题,请参考以下文章