0

I'm trying to get last location, but it always fails, here's my code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="hello" />
</LinearLayout>


MainActivity.java

package com.example.gps;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    LocationManager mLocationManager;
    TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv1);
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        String locationprovider = mLocationManager.getBestProvider(criteria, true);
        Location mLocation = mLocationManager.getLastKnownLocation(locationprovider);
        tv.setText("Last location lat:" + mLocation.getLatitude() + " long:" + mLocation.getLongitude());
    }
}


AndroidManifest.xml

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

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

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

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

</manifest>


I've sent coordinates via DDMS/emulator control window, as in next image: &

And the GPS hardware is enabled for my AVD as you may see in AVD details (hw.gps): enter image description here

And location access is enabled in emulator as you may see: enter image description here

I also tried to send coordinates via telnet, using commands:

telnet localhost 5556 
geo fix 13.24 52.31 


But unfortunately, getLastKnownLocation() method always returns null!
How to solve this ?

Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82

4 Answers4

0

Change your code like this

if(locationprovider!=null)
{
 Location mLocation = mLocationManager.getLastKnownLocation(locationprovider);
}
else
{
locationprovider =LocationManager.GPS_PROVIDER;
 Location mLocation = mLocationManager.getLastKnownLocation(locationprovider);
}
TNR
  • 5,839
  • 3
  • 33
  • 62
0

For those who are interested, I found the problem & solution :)
It's because the emulator never retrieved a location before, so last location is always null !
To avoid this, we have to get the location at least once, using this code:

public class MainActivity extends Activity implements LocationListener {
    LocationManager mLocationManager;
    TextView tv;
    Location mLocation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv1);
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        String locationprovider = mLocationManager.getBestProvider(criteria, true);
        mLocationManager.requestLocationUpdates(locationprovider, 5000, (float) 2.0, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        mLocation = location;
        showupdate();
    }

    public void onProviderDisabled(String arg0) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String a, int b, Bundle c) {
    }

    public void showupdate() {
        tv.setText("Last location lat:" + mLocation.getLatitude() + " long:" + mLocation.getLongitude());
    }
}

Then whenever the initial code posted in the question was run, it will execute correctly :)

Ashraf Bashir
  • 9,686
  • 15
  • 57
  • 82
0

in emulator you wont have any support for GPS .. it will work simply if we will pass the co-ordinates.. Thats it . in emulator we can stimulate fake gps location to test it . we cannot expect the last location from that..

itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31
0

I guess you are missing the internet permissions:

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

and seems that You are using the default emulator for android but you should use Google API and set some coordinates for your Geopoint in first place.

Gridtestmail
  • 1,459
  • 9
  • 10