【问题标题】:Could not get user location in Samsung Galaxy S3?无法在三星 Galaxy S3 中获取用户位置?
【发布时间】:2013-03-12 10:02:54
【问题描述】:

我有以下类来获取用户位置。

public class GetUserLocation extends Activity {

Timer timer1;

LocationManager locationManager = null;
LocationResult locationResult;
boolean gpsEnabled = false;
boolean networkEnabled = false;
public static Location userLocation = null;

public String checkProviders(Context context) {
    if (locationManager == null) {
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }
    try {
        gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
        ex.printStackTrace();
        gpsEnabled = false;
    }

    try {
        networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
        ex.printStackTrace();
        networkEnabled = false;
    }

    if(!gpsEnabled && !networkEnabled)
        return "No Location Service Found.\nPlease turn on your location service by\nenabling GPS or Data Service or WI-FI";

    if(gpsEnabled && !networkEnabled)
        return "No Internet Service Found.\nPlease turn on your inernet service by\nenabling Data Service or WI-FI";

    return null;

}

public boolean getLocation(Context context, LocationResult result) {
    locationResult = result;

    if (locationManager == null) {
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

    // exceptions will be thrown if provider is not permitted.
    try {
        gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {

    }

    try {
        networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    // don't start listeners if no provider is enabled
    if (!gpsEnabled && !networkEnabled)
        return false;

    if (gpsEnabled) {
        locationManager.removeUpdates(locationListenerGps);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListenerGps);
    }

    if (networkEnabled) {
        locationManager.removeUpdates(locationListenerNetwork);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,locationListenerNetwork);
    }

    timer1 = new Timer();
    //timer1.schedule(new GetLastLocation(), 30000);

    return true;
}

LocationListener locationListenerGps = new LocationListener() {

    @Override
    public void onLocationChanged(Location location) {
        timer1.cancel();

        locationResult.gotLocation(location);
        locationManager.removeUpdates(this);
        locationManager.removeUpdates(locationListenerNetwork);
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
};

LocationListener locationListenerNetwork = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        timer1.cancel();
        locationResult.gotLocation(location);
        locationManager.removeUpdates(this);
        locationManager.removeUpdates(locationListenerGps);
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
};

//  class GetLastLocation extends TimerTask {
//      @Override
//      public void run() {
//          
//          locationManager.removeUpdates(locationListenerGps);
//          locationManager.removeUpdates(locationListenerNetwork);
//          
//          Location networkLocation = null, gpsLocation = null;
//          if (gpsEnabled) {
//              //t = Calendar.getInstance().getTimeInMillis();
//              gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//          }
//          
//          if (networkEnabled) {
//              networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//          }
//          
//          // if there are both values use the latest one
//          if (gpsLocation != null && networkLocation != null) {
//              
//              if (gpsLocation.getTime() > networkLocation.getTime())
//                  locationResult.gotLocation(gpsLocation);
//              else
//                  locationResult.gotLocation(networkLocation);
//              
//              return;
//          }
//          
//          if (gpsLocation != null) {
//              locationResult.gotLocation(gpsLocation);
//              return;
//          }
//          
//          if (networkLocation != null) {
//              locationResult.gotLocation(networkLocation);
//              return;
//          }
//          
//          locationResult.gotLocation(null);
//      }
//  }

public static abstract class LocationResult {
    public abstract void gotLocation(Location location);
}

public void removeUpdates() {
    if(timer1!=null) {
        timer1.cancel();
    }
    locationManager.removeUpdates(locationListenerGps);
    locationManager.removeUpdates(locationListenerNetwork);
    userLocation = null;
    locationResult.gotLocation(null);
}

public Timer getTimer() {
    return this.timer1;
}

}

此代码适用于其他设备,但不适用于 Galaxy s3。但在测试 S3 设备中,谷歌地图正在修复用户位置。

谁能说出为什么会这样?任何提示将不胜感激。

【问题讨论】:

  • 你能添加它工作的“其他”设备(Android 操作系统,型号)吗?也许 4+ 设备有一些不同。它可以在 Android 4 模拟器上运行吗?
  • @madlymad yeh 它适用于 4.0.4、4.2 的三星 Galaxy SII 和 2.3 的 Galaxy ACE。但它什么都没有,我在 4.1.2 的 SIII 上获得了用户位置 null
  • 我也面临同样的问题。适用于除 S3 以外的所有三星手机。
  • 定时器持续时间不长。最好使用 AlarmManager
  • 我记录了一个三星的论坛问题。希望你能得到一些帮助developer.samsung.com/forum/board/thread/…

标签: android android-location android-networking userlocation


【解决方案1】:

我解决了这样的问题,安装 APP GPS Status,重置 GPS。 在以下位置找到解决方案: http://productforums.google.com/forum/#!topic/maps/q24Cq4sQAtc%5B1-25-false%5D
或者,尝试从“地图”中清除应用数据。 然后,您需要在出现提示时重新授予对位置服务的访问权限。

拥抱

【讨论】:

    猜你喜欢
    • 2013-04-12
    • 1970-01-01
    • 2012-08-28
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多