android - onLocationChanged 永远不会被调用 Emulator

标签 android android-emulator location

package com.ecsmon.android.core;

import static com.ecsmon.android.constants.Constants.log;

import java.io.IOException;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

@SuppressLint("NewApi")
public class GPSManager {

    private double currentLatitude = 0d;
    private double currentLongitude = 0d;
    private static Context mCtx;
    private Location lastLocationBestProvider = null;
    private LocationManager mLocationManager;
    private GPSListenerImpl mGPSListener;
    private com.ecsmon.android.core.LocationListener mOutListener;
    private boolean enabled = false;
    private GPSListenerImpl mNETListener;

    public GPSManager(Context ctx, com.ecsmon.android.core.LocationListener locationListener) {
    mCtx = ctx;
    mLocationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
    mOutListener = locationListener;
    }

    /**
     * Start location updates
     */
    public void start() {
    log("#### Started tracking");
    lastLocationBestProvider = getLastLocationFromBestProvider();
    if (lastLocationBestProvider != null) {
        currentLatitude = lastLocationBestProvider.getLatitude();
        currentLongitude = lastLocationBestProvider.getLongitude();
        log("lat" + currentLatitude + " long " + currentLongitude);
    } else {
        log("last loaction is null");
    }
//  mGPSListener = new GPSListenerImpl("GPS");
    mNETListener = new GPSListenerImpl("NET");

    mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 1, mNETListener);
//  mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, mGPSListener);

    }

    private class GPSListenerImpl implements LocationListener {
    private String name = "";
    public GPSListenerImpl(String name) {
        log("listener created" + name);
        this.name = name;
    }

    public void onLocationChanged(Location loc) {
        log("######### LOCATION CHANGED CALLED!!!!!!!!!!!!!!!!! ##############");
        if (loc != null) {
        log("######### location changed " + loc.getAccuracy());
        currentLatitude = loc.getLatitude();
        currentLongitude = loc.getLongitude();
        mOutListener.update(currentLongitude, currentLatitude);
        } else {
        log("location is null");
        }
    }

    public void onProviderDisabled(String provider) {
        log("provider disabled > " + name);
    }

    public void onProviderEnabled(String provider) {
        log("provider enabled > " + name);
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        log("status changed");
    }
    }

    /**
     * Return last location saved in phone or null
     * 
     * @return Location
     */
    public Location getLastLocationFromBestProvider() {
    if (!enabled) {
        return null;
    }
    try {
        LocationManager lm = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        String strLocationProvider = lm.getBestProvider(criteria, true);
        Location location = lm.getLastKnownLocation(strLocationProvider);
        if (location != null) {
        return location;
        }
        return null;
    } catch (Exception e) {
        log(e.getMessage());
        return null;
    }
    }

    /**
     * Returns human readable address from longitude and latitude
     * 
     * @param latitude
     * @param longitude
     * @return
     */
    public String getAddress(Double latitude, Double longitude) {
    if (!enabled) {
        return null;
    }
    String m = "";
    try {
        if (!Geocoder.isPresent()) {
        return null;
        }
        Geocoder geo = new Geocoder(mCtx);
        List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
        if (addresses.isEmpty()) {
        return null;
        } else {
        if (addresses.size() > 0) {
            m = addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() + ", "
                + addresses.get(0).getCountryName();
        }
        }
    } catch (IOException ie) {
        log("No connection.");
        return null;
    } catch (Exception e) {
        log("Can't read adress from this cordinates : lat = " + latitude + " long " + longitude); //
        return null;
    }
    return m;
    }

    /**
     * Removes all location updates
     */
    public void stop() {
    try {
        mLocationManager.removeUpdates(mGPSListener);
        mLocationManager.removeUpdates(mNETListener);
    } catch (Exception e) {

    }

    }

}

这是我用于获取当前位置的主类,并且 onLocationChanged 永远不会被调用。我在模拟器上进行测试,并通过模拟器控制发送模拟经度和纬度。请帮助这让我发疯:(

最佳答案

斯特凡是对的。你需要做两件事。

  1. 授予对模拟位置的访问权限。截至目前,这需要在 src/debug/AndroidManifest.xml 下的特殊 list 文件中指定。创建该 xml 并向其添加此权限:

uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"

  • 确保您的位置管理器已连接到 GPS 提供商,而不是网络提供商。该信息可以在这里找到:
  • http://developer.android.com/guide/topics/location/strategies.html#MockData

    关于android - onLocationChanged 永远不会被调用 Emulator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12935064/

    相关文章:

    Android 数据存储区 IOException 无法重命名为

    Android:在 ec2 实例上安装时如何解决模拟器错误?

    java - 如何使用 OSMdroid 从 MyLocationNewOverlay 检索地理点

    android - 在 FusedLocationProviderApi 中获取不正确的当前 GPS 位置

    java - 如何重写 Canvas (重用它)

    android - 存储状态数据的更好方式/位置( ListView )

    android - 带进度条的 adb 推/拉

    android - 创建 Galaxy S5 模拟器

    macos - Genymotion Android 模拟器在 Macbook 上滞后

    java - 获取一个国家的货币代码