Docsity

Programação para dispositivos Android - implementação de componentes e funções, Exercícios de Programação para Java

Pré-visualização parcial do texto

Baixe Programação para dispositivos Android - implementação de componentes e funções e outras Exercícios em PDF para Programação para Java, somente na Docsity!

Learn Android

Studio 4

Efficient Java-Based Android

Apps Development

Second Edition

Ted Hagos

Learn Android Studio 4

Efficient Java-Based

Android Apps Development

Second Edition

Ted Hagos

For Adrianne and Stephanie.

v

Table of Contents

Chapter 1: Android Overview .................................................................................... 1

History ........................................................................................................................................... 1 The Operating System................................................................................................................... 3 Summary....................................................................................................................................... 5

Chapter 2: Android Studio ........................................................................................ 7

Setup ............................................................................................................................................. 7 Configuring Android Studio ......................................................................................................... 10 Hardware Acceleration ................................................................................................................ 14 Summary..................................................................................................................................... 15

Chapter 3: Project Basics ....................................................................................... 17

Create a Project .......................................................................................................................... 17 Create an AVD ............................................................................................................................. 22 Summary..................................................................................................................................... 29

Chapter 4: Android Studio IDE ................................................................................ 31

The IDE ........................................................................................................................................ 31 Main Editor .................................................................................................................................. 34 Editing Layout Files ..................................................................................................................... 35 TODO Items ................................................................................................................................. 38

About the Author ..................................................................................................... xi
About the Technical Reviewer ............................................................................... xiii
Acknowledgments ...................................................................................................xv
Introduction ...........................................................................................................xvii
 - How to Get More Screen Space for Codes - Project Tool Window - Preferences/Settings - Summary..................................................................................................................................... 
  • Chapter 5: Android Application Overview............................................................... - What Makes Up an Android Project.............................................................................................
    • Application Entry Point
      • Activities
      • Intents
      • Summary.....................................................................................................................................
  • Chapter 6: Activities and Layouts - Activity - Layout File - Activity Class
    • Hello World
    • Modifying Hello World
    • Summary.....................................................................................................................................
  • Chapter 7: Event Handling - Intro to Event Handling................................................................................................................ - Handling Long Clicks - Summary.....................................................................................................................................
  • Chapter 8: Intents
    • What Intents Are..........................................................................................................................
    • Implicit Intents
    • Summary.....................................................................................................................................
  • Chapter 9: Fragments
    • Introduction to Fragments.........................................................................................................
    • Summary...................................................................................................................................
  • Chapter 10: Navigation vii - Navigation Before Architecture Components
    • Navigation Components
      • Working with Jetpack Navigation
      • Summary...................................................................................................................................
  • Chapter 11: Running in the Background............................................................... - Basic Concepts - The UI Thread - Threads and Runnables
    • Summary...................................................................................................................................
  • Chapter 12: Debugging
    • Types of Errors - Syntax Errors - Runtime Errors - Logic Errors
    • Debugger - Single Stepping
    • Summary...................................................................................................................................
  • Chapter 13: Testing
    • Types of Testing
    • Unit Testing - JVM Test vs. Instrumented Test - A Simple Demo - Implementing the Test - Running a Unit Test
    • Instrumented Testing - Setting Up a Simple Test...................................................................................................... - Recording Espresso Tests - More on Espresso Matchers - Espresso Actions
    • Summary...................................................................................................................................
  • Chapter 14: Working with Files viii - Internal and External Storage
    • Cache Directory.........................................................................................................................
      • How to Work with Internal Storage
      • Summary...................................................................................................................................
  • Chapter 15: BroadcastReceivers - Introduction to BroadcastReceivers - System Broadcast vs. Custom Broadcast............................................................................ - Two Ways to Register for Broadcast - Summary...................................................................................................................................
  • Chapter 16: Jetpack, LiveData, ViewModel, and Room - Lifecycle Aware Components - ViewModel
    • LiveData
    • Room
    • Summary...................................................................................................................................
  • Chapter 17: Distributing Apps
    • Prepare the App for Release - Prepare Material and Assets for Release - Configure the App for Release
    • Build a Release-Ready Application
    • Releasing the App
      • Summary...................................................................................................................................
  • Chapter 18: Short Takes - Productivity Features - Importing Samples - Refactoring - Generate - Coding Styles
    • Live Templates ix
    • Important Keyboard Shortcuts
    • Summary...................................................................................................................................
  • Appendix A: Java Refresher - A Brief History - Editions - Setup - Writing, Compiling, and Running - Syntax - A Typical Java Program - Compilation Unit - Comments - Statements - Keywords - Identifiers - Methods............................................................................................................................... - Packages and Imports - Program Entry Point - Data Types................................................................................................................................. - Overflow - Casting - Strongly and Statically Typed - Reference Types - Stack and Heap - Constants............................................................................................................................. - Operators - Assignment.......................................................................................................................... - Arithmetic - Unary - Equality and Relational - Logical Operators
    • Loops and Branches.................................................................................................................. x
      • If and Switch Statements
      • Switch Statement
      • While Loop
      • For-Loop
      • Simple Application of Control Structures.............................................................................
    • Arrays
      • Array Creation......................................................................................................................
      • Managing Arrays..................................................................................................................
      • Using the Enhanced for-loop
      • More on Arrays
    • Reference Types
    • Classes
      • Inheritance
      • Constructors
      • Overloading
      • Overriding
    • Strings.......................................................................................................................................
      • String Creation.....................................................................................................................
      • Strings Are Immutable
      • Why Can’t We Modify Strings
      • Comparing Strings
      • Common Usage
    • Exceptions.................................................................................................................................
  • Index

xi

About the Author

Ted Hagos is a software developer by trade. At the moment, he’s Chief Technology Officer and Data Protection Officer of RenditionDigital International, a software development company based out of Dublin. He wore many hats in his 20+ years in software development, for example, team lead, project manager, architect, and director for development. He also spent time as a trainer for IBM Advanced Career Education, Ateneo ITI, and Asia Pacific College.

xv

Acknowledgments

To Stephanie and Adrianne, my thanks and my love. To Mark Powers and Steve Anglin, and to all who made this book possible. Many, many thanks.

xvii

Introduction

Welcome to Learn Android Studio 4. This book will help you get started in your programming journey with the little green robot. You already bought the book (many thanks to you), so you don’t need to be convinced that programming for the mobile platform offers many opportunities for software developers. The book is aimed at beginning Android programmers but not wholly new to programming. The book assumes that you have prior programming experience with any of the CFOL (C family of languages, e.g., C, C++, Java, C#, JavaScript). Ideally, you are already a Java programmer trying to get your feet wet in Android; if you’re not, don’t worry. Basic Java programming is covered in the Appendix, and you can refer to that as you try to feel your way into the language. The book covers two fronts: the fundamentals of Android programming and the use of Android Studio 4. Android programming concepts and the use of the IDE are explained using a combination of graphics and code walk-throughs: there’s plenty of those in the book.

Chapter Overview

Chapter 1 : Android Overview —This chapter introduces Android. It deals with a bit of Android’s history and the technical makeup of its OS. Chapter 2 : Android Studio —If you haven’t set up your Android environment yet, don’t skip this chapter; it walks you through the setup of Android Studio, whether you’re on macOS, Windows, or Linux. It also introduces the essential parts of the IDE. Chapter 3 : Project Basics —This chapter introduces the concept and mechanics of an Android project. It walks through creating a project and running a project in an AVD (Android Virtual Device).

xix

Chapter 13 : Testing —At some point, you have to test your code before you release them. This chapter introduces you to the many kinds of testing you can do to an app. More importantly, it introduces you to unit testing and Espresso testing.

Chapter 14 : Working with Files —You’ll need to save to a text file or read from it; this chapter walks through the basics of file input and output in Android.

Chapter 15 : BroadcastReceivers —One of Android’s foundational components is the BroadcastReceiver; this component lets you build decoupled apps by adopting the publish-subscribe pattern.

Chapter 16 : Jetpack, LiveData, ViewModel, and Room —More goodies from Architecture components. This chapter walks through the basics of how to build components that have lifecycle awareness of other components and how to use Room.

Chapter 17 : Distributing Apps —When you’re ready to distribute your app, you’ll need to sign it and list it in a marketplace like Google Play. This chapter walks you through the steps on how to do it.

Chapter 18 : Short Takes —More Android Studio goodness.

Appendix —The Appendix breezes through the Java language. It deals with some of the basic language concepts you will need to get started in Android programming.

INTRODUCTION

© Ted Hagos 2020^1 T. Hagos, Learn Android Studio 4 , https://doi.org/10.1007/978-1-4842-5937-5_

CHAPTER 1

Android Overview

What the chapter covers:

  • Brief history of Android
  • The Android operating system It’s been quite a while since the little green robot made waves and disrupted the mobile computing world. It started as an operating system for phones, but it has, since, made its way into all sorts of places like TVs, car systems, watches, e-readers, netbooks, and game consoles, among other things. Android, to many people, may seem like an OS only, which for the most part it is; but apart from the OS, Android also includes a software development kit, libraries, application frameworks, and reference design.

History

2003. Andy Rubin founded Android Inc.; Google backed the company but didn’t own yet. 2005. Google bought Android Inc 2007. Android was officially given to open source; Google turned over the ownership to the Open Handset Alliance (OHA). 2008. Android v1.0 was released. The Google Play Store was called by a different name then; it was called the “Market.” 2009. Versions 1.1, 1.5 (Cupcake), 1.6 (Donut), and 2.0 (Eclair) were released. Cupcake was the first version to get the sugary treats naming scheme. This was a significant release because it featured an on-screen keyboard. Donut is remembered as the first version to include the “search box.” Eclair is remembered as the first to include Google maps, which started the death of built-in car navigation, because Google offered Maps for free.

2015. Versions 6.0 and 6.01 (Marshmallow) were released. The app menu changed dramatically, and Google added search bar so users can find apps quickly. The memory managers were introduced in this version so users can check the memory usage of apps. The permission system was revamped as well; apps can no longer request for permissions on a wholesale basis; permissions were requested (and granted) on a per-permission basis and as they were required. 2016. Versions 7.0–7.1.2 (Nougat) were released. “Google Now” was replaced with “Google Assistant.” The improved multitasking system allowed for split-screen mode. 2017. Versions 8.0 and 8.1 (Oreo) were released; and with it were more multitasking features. Picture-in-picture and native split- screen was introduced with this version. 2018. Android 9.0 (Pie) was released—exactly 10 years after v1.0. This release brought with it quite a number of visual changes which made it the most significant update in recent years. The three-button setup was replaced with a single-pill shaped button and gestures to control things like multitasking. 2019. Android 10 was released; this is a shift for Google in terms of naming the versions. Google did away with the dessert names and simply named the version according to its number. The green robot is being rebranded. This version also marks the end of the Android navigation buttons. While Android 9 kept the “back” button, v10 has completely removed it and will use gestures instead.

The Operating System

The most visible part of Android, at least for developers, is its operating system. Android OS may appear complex, but its purpose is simple; it stands between the user and the hardware. That may be an oversimplification, but it will suffice for our purposes. By “user,” I don’t literally mean an end user or a person; by “user” I mean an application, a piece of code that a programmer creates, like a word processor or an email client.

CHAPTER 1 ANDROID OVERVIEW

Take the email app, for example; as you type each character, the app needs to communicate to the hardware for the message to make its way to your screen and hard drive and eventually send it to the cloud via your network. It’s a more involved process than I describe it here, but that is the basic idea. At its simplest, an OS does three things:

  • Manages hardware on behalf of applications.
  • Provides services to applications like networking, security, memory management, and so forth.
  • Manages execution of applications; this is the part that allows us to run multiple applications (seemingly) almost at the same time. Figure 1-1 shows a logical diagram of Android’s system architecture; it is far from complete, since it doesn’t show all the apps, components, and libraries in the Android platform, but it should give you an idea on how things are organized.

The lowest level in the diagram is the one responsible for interfacing with the hardware, various services like memory management, and executions of processes. This part of the Android OS is Linux. Linux is a very stable OS and is quite ubiquitous itself. You can find it in many places like server hardware on data centers, appliances, medical devices, and so forth. Android uses Linux which handles hardware interfacing and some other kernel functions.

Figure 1-1. Platform architecture

CHAPTER 1 ANDROID OVERVIEW