我只是使用上面的代码来获取位置,它在我的测试手机Nokkia 2和Redmi 5a中都可以正常工作。但这在其他手机中不起作用 像moto g5,三星j6等
我只检查权限,一切都很好。位置权限已启用,
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
已授予这些用户权限。 仍然在某些手机中,此代码不起作用。 请帮助我找到解决方案。
private void location() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "Turn On Location", Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
return;
}else{
Toast.makeText(MainActivity.this, "in", Toast.LENGTH_SHORT).show();
LocationManager mlocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
boolean enabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
Toast.makeText(this, "please turn on your location", Toast.LENGTH_LONG).show();
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
locationManager.getProvider(LocationManager.GPS_PROVIDER);
provider = locationManager.getBestProvider(criteria, false);
if (provider != null && !provider.equals("")) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location2 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location location3 = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
final int minTime = 1000;
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minTime, 0, this);
locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, minTime, 0, this);
if (location1 != null) {
onLocationChanged(location1);
} else if (location2 != null) {
onLocationChanged(location2);
} else if (location3 != null) {
onLocationChanged(location3);
} else {
Toast.makeText(getBaseContext(), "network error..", Toast.LENGTH_SHORT).show();
Log.e("TAG", "your location can't get...." + location1);
}
} else {
Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:2)
尝试下面的代码代替LocationManager
FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});