0

// in this i'm getting location sometimes and sometimes location is not retrieving i had turned gps and gprs on and added permissions required . i'm bot getting what actually the problem is

package com.example.addr;

import java.util.List;
import java.util.Locale;

 import android.location.Address;
 import android.location.Criteria;
 import android.location.Geocoder;
 import android.location.Location;
 import android.location.LocationListener;
 import android.location.LocationManager;
 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Context;
 import android.view.Menu;
  import android.widget.TextView;


  public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LocationManager locationManager; 
    String context = Context.LOCATION_SERVICE; 
    locationManager = (LocationManager)getSystemService(context); 

    Criteria crta = new Criteria(); 
    crta.setAccuracy(Criteria.ACCURACY_FINE); 
    crta.setAltitudeRequired(false); 
    crta.setBearingRequired(false); 
    crta.setCostAllowed(true); 
    crta.setPowerRequirement(Criteria.POWER_LOW); 
    String provider = locationManager.getBestProvider(crta, true); 

 // String provider = LocationManager.GPS_PROVIDER; 
    Location location = locationManager.getLastKnownLocation(provider); 
    updateWithNewLocation(location); 

    locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); 
    } 
private final LocationListener locationListener = new LocationListener() 
{ 

@Override 
public void onLocationChanged(Location location) { 
updateWithNewLocation(location); 
} 

@Override 
public void onProviderDisabled(String provider) { 
updateWithNewLocation(null); 
} 

@Override 
public void onProviderEnabled(String provider) { 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
} 

}; 

private void updateWithNewLocation(Location location) { 
    String latLong;
    TextView myLocation; 
    myLocation = (TextView) findViewById(R.id.myLocation); 

    String addressString = "Sorry  No Address Found"; 

    if(location!=null) { 
        double lat = location.getLatitude(); 
    double lon = location.getLongitude(); 
    latLong = "Lat:" + lat + "\nLong:" + lon; 

    double lattitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 
    Geocoder gc = new Geocoder(this,Locale.getDefault()); 
    try { 
    List<Address> addresses= gc.getFromLocation(lattitude, longitude, 1); 
    StringBuilder sb = new StringBuilder(); 
    if(addresses.size()>0) { 
    Address address=addresses.get(0);
    for(int i=0;i<address.getMaxAddressLineIndex();i++)
        sb.append(address.getAddressLine(i)).append("\n");
    sb.append(address.getLocality()).append("\n"); 
    sb.append(address.getPostalCode()).append("\n"); 
sb.append(address.getCountryName()); 
} 
    addressString = sb.toString(); 
    } 
    catch (Exception e) { 
    } 
    } else { 
    latLong = " NO Location Found "; 
    } 

    myLocation.setText("Current Position is :\n"+ latLong + "\n"+  addressString ); 
       }


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


}
shenles
  • 562
  • 5
  • 19
Chaitanya Sai
  • 101
  • 1
  • 13

2 Answers2

0

GPS will not work under roof or in building you better to try with Network provider. See this post it is helpful

Community
  • 1
  • 1
Abhi
  • 8,935
  • 7
  • 37
  • 60
0

I'd recommend explicitly telling Android which location provider you want. Using the criteria can yield sketchy results. The setting of Criteria.POWER_LOW may eliminate GPS from being considered, since GPS is considered a high power drain.

For example, for the GPS provider:

String provider = LocationManager.GPS_PROVIDER; 
...
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); 

And the network provider:

String provider = LocationManager.NETWORK_PROVIDER; 
...
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
Sean Barbeau
  • 11,496
  • 8
  • 58
  • 111