0

I have followed the following link to detect the location of a device in my program

How to get Latitude and Longitude of the mobile device in android?.

But my application keeps giving the "Source not found" run time exception. Can anyone explain the reason behind it. I am also posting my code below:-

public void getLocation() { LocationManager locationmgr = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

Location location = locationmgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);

LocationListener locationListener = new LocationListener() {

    public void onLocationChanged(Location location) {
        location_longitude = location.getLongitude();
        location_latitude = location.getLatitude();
    }

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

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}

};

locationmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
Community
  • 1
  • 1
zstart
  • 19
  • 2
  • 7
  • post you error log. have you added access fine location permission – Rachita Nanda Sep 01 '14 at 07:40
  • Yes I have added the permission. In fact I have added both the permission. And actually its not an error, but a runtime exception which reads "Source not found" @RachitaNanda – zstart Sep 01 '14 at 07:56

2 Answers2

2

I used this class and successfully finds user's current location . Hope it will works for you ...

public class LocationTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude


private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public LocationTracker(Context context){
    this.mContext = context;
    getLocation();
}

public Location getLocation(){

    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;

}

/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 * */
public void stopUsingGPS(){
    if(locationManager != null){
        locationManager.removeUpdates(LocationTracker.this);
    }       
}

/**
 * Function to get latitude
 * */
public double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
        Log.d("lat", latitude+"");
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
        Log.d("long :", longitude+"");
    }

    // return longitude
    return longitude;
}


/**
 * Function to check GPS/wifi enabled
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog
 * On pressing Settings button will lauch Settings Options
 * */
public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            mContext.startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

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

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

}

and in your activity create the object of it and fetch location like as follows ....

LocationTracker locationTracker = new LocationTracker(StreamActivity.this);
if (!locationTracker.canGetLocation()) {
            locationTracker.showSettingsAlert();
        } else {

            latitude = locationTracker.getLatitude;
            longitude = locationTracker.getLongitude;


            locationTracker.stopUsingGPS();

        }

and add all the permissions in manifest file ....

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

best of luck ....

Sachin
  • 528
  • 5
  • 17
  • It keeps throwing this source not found exception. Earlier it was throwing this exception while executing "requestlocationupdates()" and now after implementing the code provided by you, I am getting the same exception while "isProviderEnabled()" is getting executed. What could be the reason behind it? Am I missing out some libraries or something?? @Sachin – zstart Sep 01 '14 at 10:21
  • you can also go through this link [http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/] @zstart – Sachin Sep 01 '14 at 11:11
  • Yes, the location service for my device is enabled. @Sachin – zstart Sep 01 '14 at 11:31
  • hey you go through above link . Check this demo is working in your device or not . It is successfully working for me . test it on device not on emulator @zstart – Sachin Sep 01 '14 at 12:16
0

The problem was that I was testing this code on a cell phone without a SIM card. Because I was using "GPS provider", I thought that it is not necessary to have a SIM card inserted on the device. But I was wrong. It worked like a charm on the device with a SIM card inserted.

zstart
  • 19
  • 2
  • 7