Jono
Jono

Reputation: 18128

Android location issue

Hi i am trying to get the device location using the Location api in android using the device network or wifi connection. However it doesnt seem to get the latt or longitude.

i have created a location provider and manager, and registered a location listener.

i then

launched

locationManagaer.requestLocationUpdates(locationProvide.getName(),TIME_INTERVAL, DISTANCE, this);

and from that point on, the app doesnt retrieve new location changes at all. what am i missing?

here is my userLocation Code

public class UserLocation implements LocationListener{

    public static final int UPDATE_LATT = 0;

    private static final int TIME_INTERVAL = 1000;

    private static final int DISTANCE = 10;

    private LocationManager locationManagaer;

    private LocationProvider locationProvide;

    private Handler mHandler;

    public UserLocation(Context context, Handler handler){
        locationManagaer = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        locationProvide = locationManagaer.getProvider(LocationManager.NETWORK_PROVIDER);

        mHandler = handler;


    }

    public void startSearchLocation(){
        Log.d("", "sart location search");
        locationManagaer.requestLocationUpdates(locationProvide.getName(),TIME_INTERVAL, DISTANCE, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d("", "location update on " + location.getLongitude());
        Message.obtain(mHandler,UPDATE_LATT, new Position(location.getLongitude(), location.getLatitude()));

    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("", "onProviderDisabled");

    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("", "onProviderEnabled");

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("", "onStatusChanged");

    }

}

here is my activity that uses this class to setup and listen for new locations

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

    mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            position = (Position) msg.obj;

            Toast.makeText(
                    getApplicationContext(),
                    position.getLattitude() + " " + position.getLongetude(),
                    Toast.LENGTH_LONG).show();
        }

    };

    userLocation = new UserLocation(this, mHandler);

    initializeViews();


    userLocation.startSearchLocation();
}

Here is my position POJO

public class Position {

    private double longetude;

    private double lattitude;

    public Position(){

    }

    public Position(double longetude, double lattitude){

    }

    public double getLongetude() {
        return longetude;
    }

    public void setLongetude(double longetude) {
        this.longetude = longetude;
    }

    public double getLattitude() {
        return lattitude;
    }

    public void setLattitude(double lattitude) {
        this.lattitude = lattitude;
    }


}

Here is part of my manifest file

  <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.jr.haliotest.MainActivity"
            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.INTERNET" />

Logcat:

03-23 20:19:14.309: D/(16992): sart location search
03-23 20:19:14.384: E/(16992): file /data/data/com.nvidia.NvCPLSvc/files/driverlist.txt: not found!
03-23 20:19:14.384: I/(16992): Attempting to load EGL implementation /system/lib//egl/libEGL_tegra_impl
03-23 20:19:14.449: I/(16992): Loaded EGL implementation /system/lib//egl/libEGL_tegra_impl
03-23 20:19:14.514: I/(16992): Loading GLESv2 implementation /system/lib//egl/libGLESv2_tegra_impl

Testing on a htc one x thats rooted if that helps. wifi is on and god signal

Upvotes: 1

Views: 1062

Answers (2)

paul
paul

Reputation: 805

I would start by checking if your phones location services are enabled and maybe try rebooting. Also try using "GPS" instead of "network" to see if the problem is related to your network not providing location info.

From my experience wifi networks will sometimes simply not provide location info. I'm not sure if that's also true for cell networks, but I suspect there is no guarantee.

Upvotes: 1

user_CC
user_CC

Reputation: 4776

If you are within a building then it is probably that your device is not able to connect to a GPS Provider. I would suggest if you move to a more open space so that you can get a signal.

One hint that if the device is connected to receive a gps signal. The sign of the gps will stop blinking and will become solid.

Upvotes: 1

Related Questions