Good day. I am trying to determine the location through the Network Provider, but nothing comes out. With GPS, everything works. below code:

public class MainActivity extends AppCompatActivity implements LocationListener {private static final String TAG = "myLog";

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); } @Override public void onLocationChanged(Location location) { if (location != null) { Log.d(TAG, "Широта="+location.getLatitude()); Log.d(TAG, "Долгота="+location.getLongitude()); } } @Override public void onProviderDisabled(String provider) { } @Override public void onProviderEnabled(String provider) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } 

}

Nothing is displayed in the logs. In the manifesto announced everything. What could be the problem? I'm testing on the phone xiaomi, can they then wise it?

  • In the manifesto prescribed? <uses-permission android: name = "android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android: name = "android.permission.ACCESS_COARSE_LOCATION" /> - ivansoft
  • Yes, everything is written. - Garic

1 answer 1

here are parts of a working example

 manifest <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> onCreate(...){ //Подключаем GPS locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); startGPS(); } private void startGPS() { 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; } locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 10, 10, locationListener); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 10, 10, locationListener); progressBarGPS.setVisibility(View.VISIBLE); Log.i(Const.TAG, "onClick: "); } private LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { Log.i(Const.TAG, "onLocationChanged: " + location.getLatitude() + "," + location.getLongitude()); //Отобразим координаты new ParseTask("" + location.getLatitude() + "," + location.getLongitude()).execute(); } @Override public void onProviderDisabled(String provider) { //checkEnabled(); //Log.i(TAG, "onProviderDisabled: "); } @Override public void onProviderEnabled(String provider) { //checkEnabled(); //showLocation(locationManager.getLastKnownLocation(provider)); //Log.i(TAG, "onProviderEnabled: "); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { //if (provider.equals(LocationManager.GPS_PROVIDER)) { // tvStatusGPS.setText("Status: " + String.valueOf(status)); //} else if (provider.equals(LocationManager.NETWORK_PROVIDER)) { // tvStatusNet.setText("Status: " + String.valueOf(status)); //} //Log.i(TAG, "onStatusChanged: "); } }; 
  • I do not know what happened but my code is working. Apparently all the same it was with the phone, because flew update and everything began to work. Thanks for the help - Garic