Reputation: 99
I need to retrieve the current location using GPS in separate thread.
Upvotes: 3
Views: 4431
Reputation: 1122
private void turnGPSOn(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){ //if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
private void turnGPSOff(){
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(provider.contains("gps")){ //if gps is enabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
}
}
Upvotes: 0
Reputation: 1623
GPS works asynchronously you don't need to call in separate thread, GPS does not freeze the User interface when getting the positions. Best is you can use a progressDialog and dismiss the progressDialog after you get the positions.
Upvotes: 0
Reputation: 29632
study following code,
public class LocListener implements LocationListener {
private static double lat =0.0;
private static double lon = 0.0;
private static double alt = 0.0;
private static double speed = 0.0;
public static double getLat()
{
return lat;
}
public static double getLon()
{
return lon;
}
public static double getAlt()
{
return alt;
}
public static double getSpeed()
{
return speed;
}
@Override
public void onLocationChanged(Location location)
{
lat = location.getLatitude();
lon = location.getLongitude();
alt = location.getAltitude();
speed = location.getSpeed();
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
now use this code for starting gps in thread.
new Thread ( new Runnable()
{
public void run()
{
locationManager = ( LocationManager ) getSystemService ( Context.LOCATION_SERVICE );
locationListener = new LocListener();
locationManager.requestLocationUpdates ( LocationManager.GPS_PROVIDER, 0, 0, locationListener );
}
}).start();
Upvotes: 1