2

Right it will run on the emulator but wont show the location. The gps is turned on in the setting as well. I got the code from a tutorial and it worked for a lot of ppl but no1 has encountered my problem. I would be grateful if someone could tell me why. Oh and im running on android 2.2

This is my code:

package gps.attempt;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class GpsAttemptActivity extends Activity implements LocationListener {

/* this class implements LocationListener, which listens for both
 * changes in the location of the device and changes in the status
 * of the GPS system.
 * */

static final String tag = "Main"; // for Log

TextView txtInfo;
LocationManager lm;
StringBuilder sb;
int noOfFixes = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /* get TextView to display the GPS data */
    txtInfo = new TextView(this);

    /* the location manager is the most vital part it allows access
     * to location and GPS status services */
    lm = (LocationManager) getSystemService(LOCATION_SERVICE);
}

@Override
protected void onResume() {
    /*
     * onResume is is always called after onStart, even if the app hasn't been
     * paused
     *
     * add location listener and request updates every 1000ms or 10m
     */
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
    super.onResume();
}

@Override
protected void onPause() {
    /* GPS, as it turns out, consumes battery like crazy */
    lm.removeUpdates(this);
    super.onResume();
}

@Override
public void onLocationChanged(Location location) {
    Log.v(tag, "Location Changed");

    sb = new StringBuilder(512);

    noOfFixes++;

    /* display some of the data in the TextView */

    sb.append("No. of Fixes: ");
    sb.append(noOfFixes);
    sb.append('\n');
    sb.append('\n');

    sb.append("Londitude: ");
    sb.append(location.getLongitude());
    sb.append('\n');

    sb.append("Latitude: ");
    sb.append(location.getLatitude());
    sb.append('\n');

    sb.append("Altitiude: ");
    sb.append(location.getAltitude());
    sb.append('\n');

    sb.append("Accuracy: ");
    sb.append(location.getAccuracy());
    sb.append('\n');

    sb.append("Timestamp: ");
    sb.append(location.getTime());
    sb.append('\n');

    txtInfo.setText(sb.toString());
}

@Override
public void onProviderDisabled(String provider) {
    /* this is called if/when the GPS is disabled in settings */
    Log.v(tag, "Disabled");

    /* bring up the GPS settings */
    Intent intent = new Intent(
            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);
}

@Override
public void onProviderEnabled(String provider) {
    Log.v(tag, "Enabled");
    Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    /* This is called when the GPS status alters */
    switch (status) {
    case LocationProvider.OUT_OF_SERVICE:
        Log.v(tag, "Status Changed: Out of Service");
        Toast.makeText(this, "Status Changed: Out of Service",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.TEMPORARILY_UNAVAILABLE:
        Log.v(tag, "Status Changed: Temporarily Unavailable");
        Toast.makeText(this, "Status Changed: Temporarily Unavailable",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.AVAILABLE:
        Log.v(tag, "Status Changed: Available");
        Toast.makeText(this, "Status Changed: Available",
                Toast.LENGTH_SHORT).show();
        break;
    }
}

@Override
protected void onStop() {
    /* may as well just finish since saving the state is not important for this toy app */
    finish();
    super.onStop();
}

}

This is my manifest:

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

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

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".GpsAttemptActivity"
        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>

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />

5
  • How are you passing your GPS location to the emulator? Commented Mar 7, 2012 at 15:50
  • Ru running in google avd emulator? do U want to add any use-libarary in your manifest!? Commented Mar 7, 2012 at 15:52
  • 1
    Did you sent GPS data to the emulator? Otherwise it will not send any events to the listener. Commented Mar 7, 2012 at 15:52
  • 1
    Nothing to do with your lack of fixes, but you shouldn't call super.onResume() in your onPause()! Commented Mar 7, 2012 at 16:06
  • @user1234167 check out zapi answer. I think it will resolve you problem. Commented Mar 7, 2012 at 16:08

2 Answers 2

4

Your problem is (at least) that your TextView is not visible. You should add a TextView in res/layout/main.xml and use that one to display the text. E.g.:

/* get TextView to display the GPS data from Layout*/
txtInfo = (TextView) findViewById(R.id.textView1);

and TextView in xml looks like

<TextView android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Edit: as NickT points out you also have to change super.onResume() to super.onPause() in your onPause()

Edit2: You don't need to use a StringBuilder to build your String. You can safely do

String text = "No. of Fixes: " + 
    noOfFixes +
    '\n' +
    // and so on
    '\n';
txtInfo.setText(text);

The Java compiler will automatically use a StringBuilder but your code looks cleaner. See e.g. when to use StringBuilder in java

Sign up to request clarification or add additional context in comments.

3 Comments

see the line saying txtInfo = new TextView(this);? If you don't add it somehow to the layout it's not visible
Hey thanks very much its finally working! Is there an easy way to now change this to reverse geocoding? Ive had a look at some of the tutorials but still a little confused
I guess you can feed developer.android.com/reference/android/location/… with location.getLongitude() and location.getLatitude(). You will also need the Internets permission I guess.
2

To use GPS in the emulator you need to manually pass the location using the geo fix command.

This guide How to enable fake GPS on Android and this SO question should help you get it working.

1 Comment

or pass it on from 'Emulator Control' pane in the DDMS perspective

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.