<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

The new Android LocationApi and permissions

Published on
12,484 Points
7,484 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
Author:Chris Harte
0 Comments

Featured Post

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month