Sachin Suryavanshi
Sachin Suryavanshi

Reputation: 524

Application crashes when I check if GPS sensor present in a sysyem?

I followed answers given for How to check for the presence of a GPS sensor?
and did changes according but in all cases my app crashes. I used

PackageManager pm = getPackageManager();
boolean hasGps = pm.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);

then again i modified my code and used

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();
}

but in both the cases my app crashes. Please tell me where m I going wrong?

Upvotes: 1

Views: 381

Answers (1)

Sachin Suryavanshi
Sachin Suryavanshi

Reputation: 524

I found mistake in my code and did required changes.
Initially my code was

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);
}

then i changed my code to

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);
}

NOTE- if provider is not available in a system then one should not attempt to start it.

Upvotes: 2

Related Questions