Reputation:
First I apologize for the duplication, I know this question was asked and I tried to follow up the existing answers, but it wont work. So if you can please see my code and say what is wrong or missing, that would help me a lot.
Here's the activity, it uses only the Log (no GUI).
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity implements LocationListener {
private LocationManager lManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0, 0, this);
else
Log.i("Test", "network provider unavailable");
Location lastKnownLocation =
lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (lastKnownLocation != null) {
Log.i("Test", lastKnownLocation.getLatitude() + ", "
+ lastKnownLocation.getLongitude());
lManager.removeUpdates(this);
} else
Log.i("Test", "null");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onLocationChanged(Location location) {
Log.i("Test", "location changed: " + location.getLatitude() + ", "
+ location.getLongitude());
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
when I try to run the app everything seems to work but latitude and longitude wont show on the log. I am using a real device with network location on for testing.
Upvotes: 1
Views: 2144
Reputation:
The reason I couldn't get the location is the cellular company does not give that information (NETWORK_PROVIDER uses cellular antenna triangulation to determine location. Cellular company can provide or deny you this information).
Upvotes: 2