Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if GPS is enabled on Android using Titanium

I am working on an Android 3.2+ app in Titanium. It is crucial that I am able to determine if the device has GPS enabled. According to the Titanium API reference, Ti.Geolocation.locationServicesEnabled will always return true on Android 2.2+, because of a new "passive" location provider. Is there any other way of determining if GPS is truly enabled?

Thanks.

like image 721
rogerlsmith Avatar asked May 14 '12 19:05

rogerlsmith


1 Answers

i think this code should work for you:

//check to see if we have GPS capabilities
if(Titanium.Geolocation.isLocationProviderEnabled(Titanium.Geolocation.PROVIDER_GPS,     Titanium.Geolocation.ACCURACY_BEST) == false) {
var alertDlg = Titanium.UI.createAlertDialog({
    title:'MileTrackGPS', 
    message:'GPS is OFF.  Enable it in Settings.',
    buttonNames: ['Cancel', 'Open Settings']
});
alertDlg.cancel = 0;

alertDlg.addEventListener('click', function(e){
    if(!e.cancel) {
        //open up the settings page
        var settingsIntent = Titanium.Android.createIntent({
            action: 'android.settings.LOCATION_SOURCE_SETTINGS'
        });
        activity.startActivity(settingsIntent);
    }
    else {
        //close the window to exit
        win.close();
    }
});

alertDlg.show();
}

refrence

like image 130
Vahid Mehrabi Avatar answered Oct 05 '22 23:10

Vahid Mehrabi