未启用 GPS 时应用程序强制关闭

发布于 2024-10-21 02:53:48 字数 2102 浏览 3 评论 0原文

我无法弄清楚为什么在未启用 GPS 时我的应用程序强制关闭。在运行前启用时,一切都很好。一切工作正常,直到我添加 LocationListener 来更新当前位置。我确信这件事简单得可笑。

 public class GlenrochieMain extends Activity

 {


   @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.main);      

    //GPS Functionality

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    //Criteria for GPS
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);      

    Location location = locationManager.getLastKnownLocation(provider);
    updateWithNewLocation(location);

    //updates location every 5second+5meters
    locationManager.requestLocationUpdates(provider, 5000, 5,
            locationListener);
  }

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider){
      updateWithNewLocation(null);
    }

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

  private void updateWithNewLocation(Location location) {
    String latLongString;
    TextView myLocationText; 
    myLocationText = (TextView)findViewById(R.id.myLocationText);
    if (location != null) {
      double lat = location.getLatitude();
      double lng = location.getLongitude();
      latLongString = "Lat:" + lat + "\nLong:" + lng;
    } else {
      latLongString = "No location found"; 
    }
    myLocationText.setText("Your Current Location is:\n" + 
                           latLongString);
  }   
}

I am having trouble figuring out why my app force closes when GPS is not enabled. When enabled before running, all is well. Everything worked fine until I added the LocationListener to update current location. I'm sure it is something ridiculously simple.

 public class GlenrochieMain extends Activity

 {


   @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.main);      

    //GPS Functionality

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    //Criteria for GPS
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);      

    Location location = locationManager.getLastKnownLocation(provider);
    updateWithNewLocation(location);

    //updates location every 5second+5meters
    locationManager.requestLocationUpdates(provider, 5000, 5,
            locationListener);
  }

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider){
      updateWithNewLocation(null);
    }

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

  private void updateWithNewLocation(Location location) {
    String latLongString;
    TextView myLocationText; 
    myLocationText = (TextView)findViewById(R.id.myLocationText);
    if (location != null) {
      double lat = location.getLatitude();
      double lng = location.getLongitude();
      latLongString = "Lat:" + lat + "\nLong:" + lng;
    } else {
      latLongString = "No location found"; 
    }
    myLocationText.setText("Your Current Location is:\n" + 
                           latLongString);
  }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福%小乖 2024-10-28 02:53:48

您需要确保 GPS 确实已启用。 此答案适用于 Android 1.6,请参阅如何检查 GPS 接收器的当前状态? 对于更高版本。

You need to make sure that the GPS is actually enabled. This answer is for Android 1.6, see How can I check the current status of the GPS receiver? for later versions.

颜漓半夏 2024-10-28 02:53:48
String provider = locationManager.getBestProvider(criteria, true);  

当没有找到合适的提供者时,getBestProvider 将返回 null。你还没有处理过这个案子。

String provider = locationManager.getBestProvider(criteria, true);  

When no suitable provider is found, getBestProvider will return null. You have not handled that case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文