Book Image

Android 9 Development Cookbook - Third Edition

By : Rick Boyer
Book Image

Android 9 Development Cookbook - Third Edition

By: Rick Boyer

Overview of this book

The Android OS has the largest installation base of any operating system in the world. There has never been a better time to learn Android development to write your own applications, or to make your own contributions to the open source community! With this extensively updated cookbook, you'll find solutions for working with the user interfaces, multitouch gestures, location awareness, web services, and device features such as the phone, camera, and accelerometer. You also get useful steps on packaging your app for the Android Market. Each recipe provides a clear solution and sample code you can use in your project from the outset. Whether you are writing your first app or your hundredth, this is a book that you will come back to time and time again, with its many tips and tricks on the rich features of Android Pie.
Table of Contents (24 chapters)
Title Page
Copyright and Credits
Dedication
About Packt
Contributors
Preface
Index

How to get the device location


This first recipe will show you how to get the last known location. If you've worked with the Google Location APIs in the past, then you may notice things have changed. This recipe shows you the latest API for getting both the last location and updates as the location changes.

Getting ready

Create a new project in Android Studio and call it GetLocation. Use the default Phone & Tablet options, and select Empty Activity when prompted for Activity Type.

How to do it...

First, we'll add the necessary permissions to the Android Manifest, then we'll modify the TextView element to include an ID. Finally, we'll add a method to receive the last known location callback. Open the Android Manifest and follow these steps:

  1. Add the following permission:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  1. Under the Gradle Scripts section, open the build.gradle (Module: app) file, as shown in this screenshot:
  1. Add the following statement to the dependencies...