0

I tried to get the current location in Android. At least, I took this example from this post.

How do I get the current GPS location programmatically in Android?

While this post is five years old, commonsware told me to ask a new question. Here is my code Snippet.

@Override
public void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    Log.i(TAG, "onCreate");
    setHasOptionsMenu(true);
    mActivity = (CustomerTabs) this.getActivity();
    mActivity.getActionBar().hide();

    if (checkGooglePlayServicesAvailable()){
        enableTabs(false);
        showDialog(true, null);
        requestPosition();
    }


boolean checkGooglePlayServicesAvailable() {
    final int connectionStatusCode = GooglePlayServicesUtil
            .isGooglePlayServicesAvailable(getActivity());
    Log.i(TAG,
            "checkGooglePlayServicesAvailable, connectionStatusCode="
                    + connectionStatusCode);
    if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
        //showDialog(false,null);
        showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
        return false;
    }
    return true;
}

void showGooglePlayServicesAvailabilityErrorDialog(
        final int connectionStatusCode) {
    getActivity().runOnUiThread(new Runnable() {
        public void run() {
            try {
                final Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
                        connectionStatusCode, getActivity(),
                        101);
                if (dialog == null) {
                    Log.e(TAG,
                            "couldn't get GooglePlayServicesUtil.getErrorDialog");
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });
}

    void requestPosition(){
    Log.i(TAG,"requestPosition");

    locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    try {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                60000, 1000,
                onLocationChange);
    }catch (Exception e){
        e.printStackTrace();
        showDialog(false, null);
        enableTabs(true);
        displayMessage(getString(R.string.position_fail));
    }
}

LocationListener onLocationChange=new LocationListener() {
    @Override
    public void onLocationChanged(android.location.Location location) {
        Log.i(TAG, "onLocationChange " + location.getLatitude() + "/" +    location.getLongitude());
        TemporaryPersister.getInstance().setCurrentPosition(new LatLng(location.getLatitude(),location.getLongitude()));
        requestLocations();
    }

permissions at manifest:

<permission
    android:name="versatec.organightandroid.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>
<uses-permission android:name="versatec.organightandroid.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES "/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

EDIT:

This solution is working on 4.1 (S3 mini) with and without WiFi but not working on another device (S3: without SIM - problem?) in WiFi. The dialog appears, requestPosition() is called - and noting happens...

I got feedback from a tester that its not working (nothing happens) on 4.2 - S4, network available.

Log:

I/Google Maps Android API﹕ Google Play services package version: 6599036
I/Map﹕ onCreate
I/Map﹕ checkGooglePlayServicesAvailable, connectionStatusCode=0
I/Map﹕ showDialog true
I/Map﹕ requestPosition
I/Map﹕ onCreateView
I/Map﹕ onActivityCreated
Community
  • 1
  • 1
Bobbelinio
  • 134
  • 1
  • 8
  • Please explain **completely and precisely** what "not on 4.2.2" means. For example, does your app crash? If so, what is the Java stack trace associated with the crash? Is the 4.2.2 device a WiFi-only tablet? – CommonsWare Jan 02 '15 at 14:53
  • May be the users turned off the location setting ? Is it consistently happening or only some times ? – Varun Verma Jan 06 '15 at 09:12
  • This works consistently on a S3 mini (4.1.2) und consistently not on a S3 (4.1.2) with the same location settings. – Bobbelinio Jan 06 '15 at 09:40
  • I don't know whats the problem... – Bobbelinio Jan 08 '15 at 11:35

1 Answers1

0

It is because : locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, .... ) You have used only Network provider ...

If you want to be sure to get location, register for other providers also.

Varun Verma
  • 542
  • 4
  • 17
  • I don't want my users to enable GPS. I want to get the current position only by network. This is possible, isn't it? – Bobbelinio Jan 06 '15 at 09:41
  • Yes, It is possible... and that explains why it does not work on : S3: without SIM ... – Varun Verma Jan 06 '15 at 09:51
  • this is only a test device. In general its necessary to have a SIM, thats ok. But it's not working on a S4 mini (4.2.2) with SIM. Why? – Bobbelinio Jan 06 '15 at 10:05