【问题标题】:onLocationChanged never gets called EmulatoronLocationChanged 永远不会被调用 Emulator
【发布时间】:2023-03-18 16:27:01
【问题描述】:
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。我在模拟器上测试,并通过模拟器控制发送模拟经度和纬度。请帮助这让我发疯:(

【问题讨论】:

  • 您是否授予在清单中接收模拟位置的权限?
  • 起初我没有,但现在添加后我注意到发送模拟位置后模拟器崩溃
  • 这次崩溃的堆栈跟踪说明了什么?

标签: android android-emulator location


【解决方案1】:

Stefan 是对的。你需要做两件事。

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

使用权限 android:name="android.permission.ACCESS_MOCK_LOCATION"

  1. 确保您的位置管理器连接到 GPS 提供商而不是网络提供商。该信息可在此处找到:

http://developer.android.com/guide/topics/location/strategies.html#MockData

【讨论】:

    【解决方案2】:

    您可以使用Emulator control 更改模拟器的 GPS 位置值。通过这样做(onLocationChanged 方法将起作用)您可以在模拟器中测试您的应用程序

    【讨论】:

    • 试了很多次都没醒,但我用真机解决了
    • 但是我检查了在模拟器控件中加载一个 kml 文件,它可以工作:) 我可以知道你使用的是哪个 IDE 吗?
    • 因此你可以在模拟器中测试它。你能在那里看到模拟器控制和GPS信息吗
    【解决方案3】:

    问题出在模拟器上,它无法工作,但在真实设备上,它在 sec 中找到了我的位置

    【讨论】:

      【解决方案4】:

      就我而言,问题在于加速度计和磁场传感器的更新频率。仅将“SensorManager.SENSOR_DELAY_FASTEST”或“SensorManager.SENSOR_DELAY_GAME”更改为“SensorManager.SENSOR_DELAY_NORMAL”,它工作正常。 奇怪的是,GPS传感器的频率可以是“SensorManager.SENSOR_DELAY_FASTEST”,没有任何问题。

      【讨论】:

        猜你喜欢
        • 2012-02-18
        • 2017-01-20
        • 1970-01-01
        • 1970-01-01
        • 2013-07-22
        • 2013-01-02
        • 2012-08-19
        • 2013-03-22
        • 1970-01-01
        相关资源
        最近更新 更多