The new Android LocationApi and permissions

Chris Harte2015 Top Expert (Most Article Points)
CERTIFIED EXPERT
A developer with over twenty years experience. Started on mainframes and Cobol, now on mobiles and Android.
Published:
A short article about problems I had with the new location API and permissions in Marshmallow
This is a follow on from a previous article I wrote about the old Android LocationListener. With Marshmallow (Android 6.0) Google is trying to deprecate the old LocationListener and enforce the use of the Google Location API. They have their reasons for this and as mere mortals we have to go along with them.

Whilst re-writing an app to run on my newly upgraded-to-Marshmallow phone, I could not get the debugger with either Eclipse or Android Studio to work. Using my preferred IDE of Eclipse I even upgraded the Android Developer Tools (ADT) to Andmore but all I could get was
 
Can't bind to local 8601 for debugger
                       ddmlib] An established connection was aborted by the software in your host machine
                       java.io.IOException: An established connection was aborted by the software in your host machine

Open in new window


Googling, once more, proved little help.

The new location activity looks like this, five new methods alongside the expected onCreate() method
public class My_location_activity extends Activity implements ConnectionCallbacks,
                                                                                OnConnectionFailedListener
                      {
                      
                          @Override protected void onCreate(Bundle savedInstanceState)
                          {
                          }
                      
                      
                          @Override protected void onStart()
                          {
                          }
                          
                          @Override protected void onStop()
                          {
                          }
                      
                          @Override public void onConnected(Bundle con_bundle)
                          {
                          }
                      
                          @Override public void onConnectionFailed(ConnectionResult provider)
                          {
                                  
                          }
                      
                          @Override public void onConnectionSuspended(int arg0)
                          {        
                      
                          }        
                      
                      
                      }

Open in new window


To use the new location API you will need an API client in the onCreate() method, which is created using some ugly code:
 
@Override protected void onCreate(Bundle savedInstanceState)
                      {
                       GoogleApiClient api_client = new GoogleApiClient.Builder(this)
                                      .addConnectionCallbacks(this)
                                      .addOnConnectionFailedListener(this)
                                      .addApi(LocationServices.API)
                                      .build();     
                      }

Open in new window


This client is then used with the onStart, onStop, onConnectionSuspended and onConnected methods. In onConnected the FusedLocationApi is used to populate the location from the API client:
 
@Override protected void onStart()
                      {
                          super.onStart();
                          api_client.connect();
                      }
                          
                      @Override protected void onStop()
                      {
                          super.onStop();
                          api_client.disconnect();
                      }
                      
                      @Override public void onConnectionSuspended(int arg0)
                      {        
                          api_client.connect();        
                      }        
                      
                      
                      @Override public void onConnected(Bundle con_bundle)
                      {
                          Location location = LocationServices.FusedLocationApi.getLastLocation(api_client);
                      }

Open in new window

This is the bare bones of the new API and I was doing something wrong. Permissions have changed in Marshmallow (Android 6.0). Setting the permissions in the manifest is no longer enough; it still has to be done but each activity in each app now has to ask for the permissions that have been requested in the manifest. There is documentation on this but, considering it is such a monumental shift of direction, I expected a lot more. There are methods on selecting individual permissions to allow the user to customise the app, but I just wanted to get it working. So in onCreate() I added

        int prems = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);        
                               
                              if (perms != PackageManager.PERMISSION_GRANTED) 
                              {    
                                  requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                                                                  Manifest.permission.INTERNET, 
                                                                  Manifest.permission.ACCESS_NETWORK_STATE}, 1);                        
                              }

Open in new window


This goes around a lot of the thinking behind checking the permissions, but I just want to do some debugging and this will allow me to do that. So if you have set a permission in the manifest, which location requires, there has to be a request to allow the user to grant it.
0
8,520 Views
Chris Harte2015 Top Expert (Most Article Points)
CERTIFIED EXPERT
A developer with over twenty years experience. Started on mainframes and Cobol, now on mobiles and Android.

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.