Click here to Skip to main content
15,922,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not getting latitude and longitude without this feature from GPS.
when I turn on feature which is on Settings>location>Use Wireless Network(On)
I can get latitude longitude ...

Is there any other way to solve this?
Any sammple code to turn on feature


I can turn on GPS satellite by this...
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);


My code is this to get latitude and longitude...

/**
 * For Getting Gps latitude longitude
 */

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

// Getting the name of the provider that meets the
// criteria
provider = locationManager.getBestProvider(criteria,
        false);

System.out.println(provider);


if (provider != null && !provider.equals("")) {

    // Get the location from the given provider
    Location location = locationManager
            .getLastKnownLocation(provider);

    locationManager.requestLocationUpdates(provider,
            20000, 1,this);

    System.out.println(location);

    if (location != null) {
        onLocationChanged(location);
        System.out.println(latitude + longitude);


    } else {
        System.out.println("Location not retrived...");
    }


    @Override
public void onLocationChanged(Location location) {
    // Getting reference to TextView tv_longitude
    latitude = location.getLatitude();
    longitude = location.getLongitude();
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub
}
Posted
Updated 17-Mar-13 4:39am
v2

1 solution

Hello Vivek,

There is good article on this here[^]. Basically you will need following permissions
XML
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" xmlns:android="#unknown"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" xmlns:android="#unknown"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" xmlns:android="#unknown"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK" xmlns:android="#unknown"></uses-permission>
Once this is declared in the manifest your app will be able to access the Wi-Fi. You can then use following code to turn on Wi-Fi
Java
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
wifiManager.setWifiEnabled(true);
wifiManager.setWifiEnabled(false);

Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900