简体   繁体   中英

Android - Mock Locations in IntelliJ Emulator Issues

I am fairly new to Android development, and I'm running into a strange issue with an app I'm trying to develop to get up-to-speed. Sometimes this app seems to work fine, but most of the times it does not. I'm trying to simply set the GPS lat/long of the device from an app; nothing more than that really. I've utilized a lot of knowledge gathered from other posts, but in the end, I'm not sure if my app doesn't work due to emulator quirks, or if it's my code. For reference, I'm using IntelliJ, I'm targeting 2.3.3, I have indeed enabled GPS on my emulator, and I am indeed setting the appropriate permissions in my manifest.

The odd thing is that it sometimes works fine when I open the emulator, then I telnet via terminal and set the geolocation via "geo fix", and then go back into the app, and it shows my GPS lat/long that I entered. But usually it just doesn't work at all. And I get the same sort of results when I attempt to do it programatically via mock-locations in the code.

Here is the code I have at the top of my onCreate() method (locationManager is a class-level field):

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.addTestProvider(LocationManager.GPS_PROVIDER, false, false, false, false, false, true, true, 0, 5);

..And when the user types in a lat/long into the UI and taps a submit-button, the following code is called after the fields are extracted and some light validation is done:

Location mLocation = new Location(LocationManager.GPS_PROVIDER);
mLocation.setLatitude(Float.valueOf(inputLat));
mLocation.setLongitude(Float.valueOf(inputLong));
mLocation.setTime(System.currentTimeMillis());

locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, mLocation);

refreshTheLocation();

And for reference, the refreshTheLocation() method is:

public void refreshTheLocation() {
    LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){
            final Location finalLoc = location;

            if (location != null) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        TextView gpsTextView = (TextView) findViewById(R.id.gps_label);
                        gpsTextView.setText("GPS DATA FOUND:\nLatitude: " + finalLoc.getLatitude() + "\nLongitude: " + finalLoc.getLongitude());
                     }
                });
            } else {
                runOnUiThread(new Runnable() {
                   public void run() {
                      TextView gpsTextView = (TextView) findViewById(R.id.gps_label);
                      gpsTextView.setText("GPS data not found");
                   }
                });
            }
        }
   };
   MyLocation myLocation = new MyLocation();
   myLocation.getLocation(this, locationResult);
}  

The "MyLocation" class is simply a copy/paste from the top-rated answer on this post:

What is the simplest and most robust way to get the user's current location on Android?

Any thoughts on what my problem could be? It seems about 1/5 of the time, it gets the GPS coordinates and shows them, but the other 4/5 of the time, it does not. And when it DOES work, it never updates the coordinates when I change them via the input fields and button.

Go easy on me, please, as I'm fairly new to this stuff. ;) Thanks in advance for any help you guys can offer!

如果您以2.3.3版为目标并且已经在2.3.3版AVD上进行过测试,建议您切换到API的2.3.3 Google API版本,然后尝试模拟位置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM