I am trying to create a simple app where some Location parameters are displayed(Latidute,Longitudem,Provider etc). It is all fine when I use the NETWORK_PROVIDER

So far so good. When I click the "Change Location Provider Settings" button to select the GPS,the appropiate values are not shown..

Here is my code.

public class CurrentLocationActivity extends Activity implements LocationListener
{

   private LocationManager locationManager;
   private TextView latitudeValue;
   private TextView longitudeValue;
   private TextView providerValue;
   private TextView accuracyValue;
   private TextView timeToFixValue;
   private TextView enabledProvidersValue;
   private long uptimeAtResume;
private List<String> enabledProviders;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.current_location);

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    latitudeValue = (TextView) findViewById(R.id.latitudeValue);
    longitudeValue = (TextView) findViewById(R.id.longitudeValue);
    providerValue = (TextView) findViewById(R.id.providerValue);
    accuracyValue = (TextView) findViewById(R.id.accuracyValue);
    timeToFixValue = (TextView) findViewById(R.id.timeToFixValue);
    enabledProvidersValue = (TextView) findViewById(R.id.enabledProvidersValue);
}

@SuppressLint("NewApi")
@Override
protected void onResume()
{
    super.onResume();

    StringBuffer stringBuffer = new StringBuffer();

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);

    enabledProviders = locationManager.getProviders(criteria, true);

    if (enabledProviders.isEmpty())
    {
        enabledProvidersValue.setText("");
    }
    else
    {
        for (String enabledProvider : enabledProviders)
        {
            stringBuffer.append(enabledProvider).append(" ");

            locationManager.requestSingleUpdate(enabledProvider,
                                                this,
                                                null);
        }

        enabledProvidersValue.setText(stringBuffer);
    }

    uptimeAtResume = SystemClock.uptimeMillis();

    latitudeValue.setText("");
    longitudeValue.setText("");
    providerValue.setText("");
    accuracyValue.setText("");
    timeToFixValue.setText("");

    findViewById(R.id.timeToFixUnits).setVisibility(View.GONE);
    findViewById(R.id.accuracyUnits).setVisibility(View.GONE);
}

@Override
protected void onPause()
{
    super.onPause();

    locationManager.removeUpdates(this);
}

/**
 * Updates the display with the  new location information if new location
 * information is more accurate than the current location information.
 * 
 * @param location The new location information
 * 
 * @see android.location.LocationListener#onLocationChanged(android.location.Location)
 */
@Override
public void onLocationChanged(Location location)
{
    latitudeValue.setText(String.valueOf(location.getLatitude()));
    longitudeValue.setText(String.valueOf(location.getLongitude()));
    providerValue.setText(String.valueOf(location.getProvider()));
    accuracyValue.setText(String.valueOf(location.getAccuracy()));

    long timeToFix = SystemClock.uptimeMillis() - uptimeAtResume;

    timeToFixValue.setText(String.valueOf(timeToFix / 1000));

    findViewById(R.id.timeToFixUnits).setVisibility(View.VISIBLE);
    findViewById(R.id.accuracyUnits).setVisibility(View.VISIBLE);
}

/**
 * @see android.location.LocationListener#onProviderDisabled(java.lang.String)
 */
@Override
public void onProviderDisabled(String provider)
{
    // no-op
}

/**
 * @see android.location.LocationListener#onProviderEnabled(java.lang.String)
 */
@Override
public void onProviderEnabled(String provider)
{
    // no-op
}

/**
 * @see android.location.LocationListener#onStatusChanged(java.lang.String,
 *      int, android.os.Bundle)
 */
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{

}

public void onChangeLocationProvidersSettingsClick(View view)
{
    startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
}

And the manifest file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.currentlocation"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
       android:minSdkVersion="8"
       android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.currentlocation.CurrentLocationActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Maybe there is something wrong with my phone!!

Related posts

Recent Viewed