为什么,onlocationchanged no 它在使用数据移动时被调用?
Why, onlocationchanged no it's called when using data mobile?
当我使用 gps 或 wifi 时 onlocationchanged 工作正常,但是当我使用数据移动时,onlocationchanged 它只被调用一次并且位置提供它很糟糕。
我希望你能帮助我,我真的需要用数据移动获取位置。
这是我的代码:
public class ObtenerLatLng implements LocationListener {
public static double latitude = 0.0;
public static double longitude = 0.0;
public static boolean flaglocation=true;
int contador;
Context context;
LocationManager locationManager;
public ObtenerLatLng(Context context) {
this.context = context;
obtenerlatlng(context);
}
public void obtenerlatlng(Context context) {
Log.i("hola ", "Se HA SOLICITADO LOCALIZACION");
NetworkInfo aux;
NetworkInfo isWifiOn = null;
NetworkInfo isMobiledataOn = null;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network[] todas = cm.getAllNetworks();
for (Network item : todas) {
aux = cm.getNetworkInfo(item);
if (aux.getType() == ConnectivityManager.TYPE_WIFI) {
isWifiOn = aux;
}
if (aux.getType() == ConnectivityManager.TYPE_MOBILE) {
isMobiledataOn = aux;
}
}
} else {
isWifiOn = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
isMobiledataOn = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
}
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
if (isMobiledataOn.isConnected() || isWifiOn.isConnected() ) {
Toast.makeText(context, "entre a Network", Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 1, this);
}
} else {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 1, this);
}
} else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(context, "entre a gps", Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 1, this);
}
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 1, this);
}
}
} else {
flaglocation=false;
// Toast.makeText(context, "Revise la configuracion de red", Toast.LENGTH_LONG).show();
}
}
@Override
public void onLocationChanged(Location location) {
if (location!= null ) {
flaglocation=true;
Toast.makeText(context, "Se actualizo la localizacion " + location.getLatitude() + " " + location.getLongitude(), Toast.LENGTH_LONG).show();
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
使用移动数据,您只能获得手机位置。请参阅 this 了解如何接收小区位置。
Network_provider 至少需要 WiFi 才能接收网络或粗略的位置。
当我使用 gps 或 wifi 时 onlocationchanged 工作正常,但是当我使用数据移动时,onlocationchanged 它只被调用一次并且位置提供它很糟糕。
我希望你能帮助我,我真的需要用数据移动获取位置。
这是我的代码:
public class ObtenerLatLng implements LocationListener {
public static double latitude = 0.0;
public static double longitude = 0.0;
public static boolean flaglocation=true;
int contador;
Context context;
LocationManager locationManager;
public ObtenerLatLng(Context context) {
this.context = context;
obtenerlatlng(context);
}
public void obtenerlatlng(Context context) {
Log.i("hola ", "Se HA SOLICITADO LOCALIZACION");
NetworkInfo aux;
NetworkInfo isWifiOn = null;
NetworkInfo isMobiledataOn = null;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network[] todas = cm.getAllNetworks();
for (Network item : todas) {
aux = cm.getNetworkInfo(item);
if (aux.getType() == ConnectivityManager.TYPE_WIFI) {
isWifiOn = aux;
}
if (aux.getType() == ConnectivityManager.TYPE_MOBILE) {
isMobiledataOn = aux;
}
}
} else {
isWifiOn = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
isMobiledataOn = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
}
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
if (isMobiledataOn.isConnected() || isWifiOn.isConnected() ) {
Toast.makeText(context, "entre a Network", Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 1, this);
}
} else {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 1, this);
}
} else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(context, "entre a gps", Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 1, this);
}
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 1, this);
}
}
} else {
flaglocation=false;
// Toast.makeText(context, "Revise la configuracion de red", Toast.LENGTH_LONG).show();
}
}
@Override
public void onLocationChanged(Location location) {
if (location!= null ) {
flaglocation=true;
Toast.makeText(context, "Se actualizo la localizacion " + location.getLatitude() + " " + location.getLongitude(), Toast.LENGTH_LONG).show();
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
使用移动数据,您只能获得手机位置。请参阅 this 了解如何接收小区位置。
Network_provider 至少需要 WiFi 才能接收网络或粗略的位置。