1

There are some questions regarding this issue but I could not find any method works without problem. I implemented the version of the project with GPS provider and now I want to implement a version which can find the location of user without GPS, just using network provider such as Wifi. I tried this but it is not working (I could not get the "done" message in any way):

public class MapsActivity extends FragmentActivity {

    double latitude;
    double longitude;

    LocationManager locationManager;
    android.location.LocationListener networkLocationListener;
    Location location;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        networkLocationListener = new android.location.LocationListener() {
            public void onLocationChanged(Location location) {

                latitude = location.getLatitude();
                longitude = location.getLongitude();

                Log.d("->", "done.");

            }

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

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}
        };

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, networkLocationListener);

    }

...

Also, this is my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.doruk.myapplication">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!--
 The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/orange"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".ConnectionChangeReceiver"
            android:label="NetworkConnection">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

Can you see the problem? What you advise at this point? Is there a better way?

Dorukhan Arslan
  • 2,676
  • 2
  • 24
  • 42

2 Answers2

3

Dorukhan Arslan - You can use GoogleclientAPi, as locationmanager has been Deprecated -Using FusedLocationAPi it will automatically discover the provider to find the location. - https://developer.android.com/google/auth/api-client.html have a look on this - Here is one exampleprotected void createLocationRequest() { LocationRequest mLocation = LocationRequest.create(); GoogleAPiClient mgoogle; /*enter code here * Set the update interval */ mLocation.setSmallestDisplacement(500); // 2km mLocation.setInterval(180000);// 3 min mLocationRequest.setFastestInterval(120000);// 2 min mLocation.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); if (mgoogle == null) { mgoogle = new GoogleApiClient.Builder(this) .addApi(LocationServices.API).addConnectionCallbacks(this) .addOnConnectionFailedListener(this).build(); mgoogle.connect(); } }

  • It's a sample and implementing OnConnectionFailedListener, ConnectionCallbacks, com.google.android.gms.location.LocationListener above 3 interfaces you can get the continuos Location.
  • getLastLocation Method Of LocationRequest Class will give you Location.
  • onLocationChanged Method will give you Your desired output.
Pang
  • 9,564
  • 146
  • 81
  • 122
Hardy
  • 2,576
  • 1
  • 23
  • 45
  • Informative answer but that is not what I am looking for. I need to find a way to get location without GPS, so indirectly without location services. I need to get location merely using Wifi connection. If you checked Yandex Maps for instance, when you close your location services but keep open Wifi, it continues to get your current location and shows it on map. Consider two way to get location: 1. location services, 2. Wifi. Yandex Maps works with only 1 or 2 and with both 1 and 2 but my current application works with 1 and with the existence of both 1 and 2, should work with only 2 as well. – Dorukhan Arslan Mar 14 '15 at 10:04
  • @DorukhanArslan [link](http://stackoverflow.com/questions/6694391/android-get-current-location-of-user-without-using-gps-or-internet) are you asking something like this? – Hardy Mar 14 '15 at 10:13
  • Both your answer and the answers are given in this link have to use location services strictly which consumes battery overly. I test my application with Asus Zenphone and this phone have no tab for GPS. Namely, it triggers GPS when option of location services is openned. The code I already wrote but did not put here is the completed version of the technique you advised. The code I put on the question is same with you offered in this link. Unfortunalety, both of them depend on existence of location services. I need to get location if and only if the Wifi connection is active merely. – Dorukhan Arslan Mar 14 '15 at 10:38
  • By the way, I may not express myself cleary. I just want to get location when GPS, location services and other stuff are close but the Wifi connected is open. I cannot find any method to do this up to now. If you want, I can put whole project on the question as well. – Dorukhan Arslan Mar 14 '15 at 10:44
  • No not needed I am also finding you answer i will reply asap i get answer – Hardy Mar 14 '15 at 10:46
  • Big thanks to you. I am confused with the opinion of people who say the technique I wrote on the question does not need location services. I am pretty sure that if I close location services, it returns nothing. – Dorukhan Arslan Mar 14 '15 at 10:57
  • Hey even i also talked to my seniors about it but he also suggested you have to use location service otherwise You will not get the location -If my technique as per you said consumes more battery you can change the parameters like timing and distance thanks if i get another way as per your requirement i will reply asap – Hardy Mar 14 '15 at 11:06
  • So, what is the difference between your technique depends on GoogleApiClient and the other one depends on just android.location.LocationListener (namely, NETWORK or GPS_PROVIDER)? Since both use location services, I need to make a trade off between these two. In addition, you are a great help, I must say. If you have some doubts about what I am asking about please turn off your location services and open Google Maps app. It will find your location in a way. – Dorukhan Arslan Mar 14 '15 at 11:25
0

You can use IP address of user for getting approx location.

kiturk3
  • 549
  • 10
  • 30