Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Android™ Development

Reply
Contributor
Posts: 16
Registered: ‎01-16-2013
My Device: BB10 Dev Alpha
My Carrier: Free

Re: How to get GPS Location

Yes, Location Services is On in the phone settings.

 

However, my application doesn't show up in the application list behind the authorization button on that screen.

Contributor
Posts: 16
Registered: ‎01-16-2013
My Device: BB10 Dev Alpha
My Carrier: Free

Re: How to get GPS Location

For the location updates, I tried with this :

 

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);

 

But didn't get any update.

 

I tried just now with the GPS provider, and it seems to work on a Z10.

 

I tried with the Network provider because the application was supposed to work only with android.permission.ACCESS_COARSE_LOCATION and not android.permission.ACCESS_FINE_LOCATION, but the application seems to receive GPS location updates even without the android.permission.ACCESS_FINE_LOCATION permission.

Contributor
Posts: 16
Registered: ‎01-16-2013
My Device: BB10 Dev Alpha
My Carrier: Free

Re: How to get GPS Location

OK. Launched the app too fast after removing the permission, it was not redeployed yet. Cannot register for updates from GPS provider without android.permission.ACCESS_FINE_LOCATION which is what I expected.

Contributor
Posts: 16
Registered: ‎01-16-2013
My Device: BB10 Dev Alpha
My Carrier: Free

Re: How to get GPS Location

To sum up my tests, in case it helps others, here is the code for a simple test app I used on my Z10 :

import java.util.List;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initLocation();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    private void initLocation() {
        final LocationManager lm = (LocationManager)getSystemService(LOCATION_SERVICE);
        final List<String> providers = lm.getAllProviders();
        Location location = null;
        if (providers!=null) {
            for (int i = 0; i < providers.size() && location==null; i++) {
                String provider = providers.get(i);
                try {
                    location = lm.getLastKnownLocation(provider);
                    Log.d("LocationTest","Provider "+provider+". Location="+location);
                } catch (Exception e) {
                    Log.d("LocationTest","Failed to use provider "+provider);
                }
            }
            Thread updateThread = new Thread() {
                @Override
                public void run() {
                    Looper.prepare();
                    for (int i = 0; i < providers.size(); i++) {
                        String provider = providers.get(i);
                        try {
                            LocationListener listener = new LocationUpdateListener(provider, lm);
                            lm.requestLocationUpdates(provider, 0, 0f, listener);
                        } catch (Exception e) {
                            Log.d("LocationTest","Failed to use provider "+provider);
                        }
                    }
                    Looper.loop();
                }
            };
            updateThread.start();
        }
    }
    
    private class LocationUpdateListener implements LocationListener {
        
        private final String provider;
        private final LocationManager lm;

        public LocationUpdateListener(String provider, LocationManager lm) {
            this.provider = provider;
            this.lm = lm;
        }
        
        @Override
        public void onLocationChanged(Location location) {
            lm.removeUpdates(this);
            Double lat = location.getLatitude() * 1e6;
            Double lon = location.getLongitude() * 1e6;
            Log.d("LocationTest","LOCATION provider "+this.provider+" : lat " + lat + " / lon " + lon);
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d("LocationTest","LOCATION provider "+provider+" status update "+status);
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.d("LocationTest","LOCATION provider "+provider+" enabled");
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.d("LocationTest","LOCATION provider "+provider+" disabled");
        }
    }
}

With just android.permission.ACCESS_COARSE_LOCATION, it prints :

04-22 16:29:44.595: D/LocationTest(16539745): Provider network. Location=null
04-22 16:29:44.598: D/LocationTest(16539745): Failed to use provider passive
04-22 16:29:44.601: D/LocationTest(16539745): Failed to use provider gps
04-22 16:29:44.614: D/LocationTest(16539745): Failed to use provider passive
04-22 16:29:44.616: D/LocationTest(16539745): Failed to use provider gps

With the additional android.permission.ACCESS_FINE_LOCATION, it prints :

04-22 16:31:22.343: D/LocationTest(16736353): Provider network. Location=null
04-22 16:31:22.344: D/LocationTest(16736353): Provider passive. Location=null
04-22 16:31:22.346: D/LocationTest(16736353): Provider gps. Location=null
04-22 16:31:23.901: D/LocationTest(16736353): LOCATION provider gps : lat 4.887364620999999E7 / lon 2333285.7700000005
04-22 16:31:23.905: D/LocationTest(16736353): LOCATION provider passive : lat 4.887364620999999E7 / lon 2333285.7700000005

Retired
Posts: 140
Registered: ‎04-19-2013
My Device: BlackBerry Z10

Re: How to get GPS Location

As per the following thread (and a few others too, actually), NETWORK_LOCATION provider support is being looked at by development.

 

http://supportforums.blackberry.com/t5/Android-Runtime-Development/Can-not-fetch-location-using-netw...

 

And for a bit of background, the way that the NETWORK_LOCATION provider is implemented on stock Android devices is through Android's Google Services Framework.  That is, it is not included as part of open source Android.  However, the native location manager in BB10 does support it, and so it's simply a matter of hooking it up.

Contributor
Posts: 16
Registered: ‎01-16-2013
My Device: BB10 Dev Alpha
My Carrier: Free

Re: How to get GPS Location

Thanks for the update.

 

One other thing that is surprising is the following sequence (with GPS provider) :

 

- Start the app : getLastKnownLocation returns null

- requestLocationUpdates

- Listener is called with new location from GPS provider

- Close app

- Start the app again : getLastKnownLocation returns null again, even though the GPS provider had a location a few seconds earlier

 

Is it because the Android context is discarded when the app is closed ? Or for some other reason ? Or am I doing something wrong here ?

Retired
Posts: 140
Registered: ‎04-19-2013
My Device: BlackBerry Z10

Re: How to get GPS Location

Just to clarify, you're attempting to get the last known location for the GPS provider, and not the network location provider?

Contributor
Posts: 16
Registered: ‎01-16-2013
My Device: BB10 Dev Alpha
My Carrier: Free

Re: How to get GPS Location

Yes, from the GPS provider.

Retired
Posts: 140
Registered: ‎04-19-2013
My Device: BlackBerry Z10

Re: How to get GPS Location

I'm not sure why that's failing for you.  getLastKnownLocation(<provider>) is basically a HashMap lookup with the <provider> as the key.

 

LocationManagerService doesn't cull this map of "stale" entries, so the cases that getLastKnownLocation returns null is if it doesn't have an update for the provider requested.


Perhaps you'd post a bit of sample code?  I just tested out a sample app and it seemed to work fine.

Contributor
Posts: 16
Registered: ‎01-16-2013
My Device: BB10 Dev Alpha
My Carrier: Free

Re: How to get GPS Location

Seems to be working fine now... quite strange since there was no change to the code since yesterday when it wasn't returning a location after restarting the app.

 

I'll let you know if it happens again and I have an idea about what changed between the 2 tests.

 

Just looking forward to the NetworkProvider being hooked up Smiley Happy