<

The new Android LocationApi and permissions

Published on
11,915 Points
6,915 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

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Join & Write a Comment

An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month