我按照How to check for the presence of a GPS sensor?给出的答案 并做了改变,但在所有情况下我的应用程序崩溃。 我用了
PackageManager pm = getPackageManager();
boolean hasGps = pm.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
然后我再次修改了我的代码并使用了
LocationManager lm = (LocationManager)getSystemService(LOCATION_SERVICE);
if (lm.getProvider(LocationManager.GPS_PROVIDER) == null) {
Toast.makeText(getApplicationContext(),
"GPS sensor is not available", Toast.LENGTH_LONG).show();
}
但在这两种情况下我的应用程序都崩溃了。请告诉我我哪里错了?
答案 0 :(得分:2)
我在代码中发现错误,并且需要进行更改 最初我的代码是
if((ProvidePreference.equalsIgnoreCase("BestAvailable")) {
//start both gps and network
this.LocationMngr.requestLocationUpdates(
LocationManager.GPS_PROVIDER,captureFrequencey, 100, this);
this.LocationMngr.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,captureFrequencey, 0, this);
}
然后我将我的代码更改为
if((ProvidePreference.equalsIgnoreCase("BestAvailable") &&
LocationMngr.getProvider(LocationManager.GPS_PROVIDER) != null)) {
//start both gps and network
this.LocationMngr.requestLocationUpdates(
LocationManager.GPS_PROVIDER,captureFrequencey, 100, this);
this.LocationMngr.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,captureFrequencey, 0, this);
} else {
//start network
this.LocationMngr.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,captureFrequencey, 0, this);
}
注意 - 如果系统中没有提供程序,则不应尝试启动它。