After reviewing some articles on Stack Overflow, I could not get solutions to my question. As usual, I am always getting null in return of getLastKnownLocation
. I have implemented LocationListener as well. Below is code.
For your information, I have checked both providers (network and GPS) in below code and they are always returning false however if i check value of sBestProvider then I am getting "Network". How it is coming?
Permissions.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
JAVA Code
public class ActivityMarkingLocation extends Activity implements LocationListener {
GoogleMap oMap ;
LocationManager oLocationManager;
ConnectionDetector oConnectionDetector ;
Location oLocation;
Button oBtnGetCurrentLocation;
String sBestProvider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marking_location);
oLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria oCriteria = new Criteria();
sBestProvider = oLocationManager.getBestProvider(oCriteria, false);
boolean b =oLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
System.out.println("GPS Status " + b);
boolean c = oLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
System.out.println("Network Status " + c);
System.out.println("Provider " + sBestProvider);
oLocation = oLocationManager.getLastKnownLocation(sBestProvider);
System.out.println("Provider : " + sBestProvider);
System.out.println("LocationManager : " + oLocationManager);
if (oLocation == null)
System.out.println("Location Null " );
else
System.out.println("Location not null");
oBtnGetCurrentLocation = (Button) findViewById(R.id.btnGetCurrentLocation);
oBtnGetCurrentLocation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
oLocation = oLocationManager.getLastKnownLocation(sBestProvider);
if (oLocation != null)
System.out.println("Location found");
if (oLocation != null)
System.out.println("Position : " + oLocation.getLatitude() + " " + oLocation.getLongitude());
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
oLocationManager.requestLocationUpdates(sBestProvider, 600, 10, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_landing_screen, menu);
return true;
}
public boolean isGoogleMapsInstalled()
{
try
{
ApplicationInfo info = getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0 );
return true;
}
catch(PackageManager.NameNotFoundException e)
{
return false;
}
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
oLocation = arg0;
System.out.println("Location changed");
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}