I'm trying to get location on android device the code is as follows:
private void openLocation() {
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria c = new Criteria();
pro = lm.getBestProvider(c,false);
Location l = lm.getLastKnownLocation(pro);
if (l != null) {
Toast.makeText(WebActivity.this, "Longitude : "+String.valueOf(l.getLongitude())+", Latitude : "+String.valueOf(l.getLatitude()), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(WebActivity.this,"loc object is null",Toast.LENGTH_LONG).show();
}
mgac = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
but it crashes when executed. What's wrong in the code, or please give me a working code that give me location to string and I want to send the data to another url using get/post request and this request should run in background.