<

Improve company productivity with a Business Account.Sign Up

x

The new Android LocationApi and permissions

Published on
11,634 Points
6,634 Views
Last Modified:
Chris Harte
A developer with over twenty years experience. Started on mainframes and Cobol, now on mobiles and Android.
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
Comment
Author:Chris Harte
0 Comments

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Join & Write a Comment

In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Simple Linear Regression
Next Article:

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month