我不确定但是这个错误和我的应用程序之间没有任何连接不返回一些Android手机用户的位置,在一些Android手机中它完美的工作但在联想k5和其他手机它不工作,而不是返回位置
import android.Manifest;
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.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;
import java.util.List;
public class LocationForAlive implements LocationListener {
private Context context;
private LocationManager locationManager;
private String mProvider;
private String GPS = "gps";
private String NETWORK = "network";
private String PASSIVE = "passive";
private double latitude;
private double longitude;
private double arr[] = new double[2];
private static final String TAG = "Location";
LocationForAlive(Context context) {
this.context = context;
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setCostAllowed(false);
mProvider = locationManager.getBestProvider(criteria, false);
List<String> providersList = locationManager.getAllProviders();
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
boolean isPassiveEnabled = locationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER);
if (isGPSEnabled || isNetworkEnabled || isPassiveEnabled) {
Location location = returnLocation(LocationManager.GPS_PROVIDER);
if(location == null){
location = returnLocation(LocationManager.NETWORK_PROVIDER);
if(location == null){
location = returnLocation(LocationManager.PASSIVE_PROVIDER);
if(location == null){
Log.d(TAG, "Error");
Toast.makeText(context, "Error!!!!", Toast.LENGTH_LONG).show();
}else{
onLocationChanged(location);
}
}else{
onLocationChanged(location);
}
}else{
onLocationChanged(location);
}
}
}
public double[] getLocation(){
arr[0] = latitude;
arr[1] = longitude;
return arr;
}
private Location returnLocation(String providerName){
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, 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 null;
}
locationManager.requestLocationUpdates(providerName, 2000, 10, this);
Location location = locationManager.getLastKnownLocation(providerName);
if (location != null) {
return location;
}else{
return null;
}
}
@Override
public void onLocationChanged(Location location) {
if(location!=null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}