Reader small image

You're reading from  Android 9 Development Cookbook - Third Edition

Product typeBook
Published inOct 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788991216
Edition3rd Edition
Languages
Tools
Right arrow
Author (1)
Rick Boyer
Rick Boyer
author image
Rick Boyer

Rick Boyer has been programming professionally for over 20 years. He has written apps on Windows, created websites, and coded for various mobile devices, including Windows CE, Windows Phone, and Android. Almost eight years ago, he took the plunge and started his own software consulting business, NightSky Development, focusing exclusively on Android development.
Read more about Rick Boyer

Right arrow

Chapter 15. Getting Your App Ready for the Play Store

In this chapter, we will cover the following topics:

  • The Android 6.0 Runtime Permission Model
  • How to schedule an alarm
  • Receiving notification of device boot
  • Using AsyncTask for background work
  • Adding speech recognition to your app
  • How to add Google sign-in to your app

Introduction


As we approach the end of this book, it's time to add the finishing touches to your application before releasing it to the Play Store. The recipes in this chapter cover the topics that can make a difference between users keeping your app or removing it.

Our first recipe, The Android 6.0 Runtime permission model, is certainly an important topic, possibly being the primary reason Android went from version 5.x to version 6! Changes to the Android permission model have been requested for some time, so this new model is a welcome change, at least for users.

Next, we'll take a look at alarms in How to schedule an alarm. One of the primary benefits of alarms is that the OS is responsible for maintaining the alarm, even when your application is not running. Since alarms do not persist after rebooting the device, we'll also look at how to detect a device reboot so you can recreate your alarms in Receiving notification of device boot.

Almost any serious Android application will need a way...

The Android 6.0 Runtime Permission Model


The old security model was a sore point for many in Android. It's common to see reviews commenting on the permissions an app requires. Sometimes, permissions were unrealistic (such as a Flashlight app requiring internet permission), but other times, the developer had good reasons to request certain permissions. The main problem was that it was an all-or-nothing prospect.

This finally changed with the Android 6 Marshmallow (API 23) release. The new permission model still declares permissions in the manifest as before, but users have the option of selectively accepting or denying each permission. Users can even revoke a previously granted permission.

Although this is a welcome change for many, for a developer, it has the potential to break the code that was working before. We've talked about this permission change in the previous recipes, as it has far-reaching implications. This recipe will put it all together to serve as a single point of reference...

How to schedule an alarm


Android provides AlarmManager to create and schedule alarms. Alarms offer the following features:

  • Schedule alarms for a set time or interval
  • Maintained by the OS, not your application, so alarms are triggered even if your application is not running or the device is asleep
  • Can be used to trigger periodic tasks (such as an hourly news update), even if your application is not running
  • Your app does not use resources (such as timers or background services), since the OS manages the scheduling

Alarms are not the best solution if you need a simple delay while your application is running (such as a short delay for a UI event.) For short delays, it's easier and more efficient to use a Handler, as we've done in several previous recipes.

When using alarms, keep these best practices in mind:

  • Use as infrequent an alarm timing as possible
  • Avoid waking up the device
  • Use as imprecise timing as possible; the more precise the timing, the more resources required
  • Avoid setting alarm times based...

Receiving notification of device boot


Android sends out many intents during its lifetime. One of the first intents sent is ACTION_BOOT_COMPLETED. If your application needs to know when the device boots, you need to capture this intent.

This recipe will walk you through the steps required to be notified when the device boots.

Getting ready

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

How to do it...

To start, open the Android Manifest and follow these steps:

  1. Add the following permission:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
  1. Add the following <receiver> to the <application> element, at the same level as the existing <activity> element:
<receiver android:name=".BootBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <category android:name...

Using the AsyncTask for background work


Throughout this book, we have mentioned the importance of not blocking the main thread. Performing long running operations on the main thread can cause your application to appear sluggish, or worse, hang. If your application doesn't respond within about 5 seconds, the system will likely display the Application Not Responding (ANR) dialog with the option to terminate your app. (This is something you will want to avoid as it's a good way to get your app uninstalled.)

Android applications use a single thread model with two simple rules, as follows:

  • Don't block the main thread
  • Perform all UI operations on the main thread

When Android starts your application, it automatically creates the main (or UI) thread. This is the thread from which all UI operations must be called. The first rule is "don't block the main thread." This means that you need to create a background, or a worker, thread for any long-running or potentially-blocking task. This is why all network...

Adding speech recognition to your app


Android 2.2 (API 8) introduced speech recognition in Android, and it continues to improve with almost every new major Android release. This recipe will demonstrate how to add speech recognition to your app using the Google Speech service.

Getting ready

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

How to do it...

We'll start by adding a Speak Now (or microphone) button to the layout, then we'll add the necessary code to call the speech recognizer. Open activity_main.xml and follow these steps:

  1. Replace the existing TextView with the following XML:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageButton...

How to add Google sign-in to your app


Google sign in allows your users to sign in to your application using their Google credentials. This option offers several advantages to your user, including the following:

  • Confidence because they're using Google
  • Convenience since they can use their existing account

There are also several advantages for you, the developer:

  • Convenience of not having to write your own authentication server
  • More users logging in to your app 

This recipe will walk you through the process of adding Google sign-in to your application. Here's a screenshot showing the "GoogleSignin" button in the application that we'll create in the recipe:

Getting ready

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

Google sign-in uses the Google Services plugin, which requires a Google Services Configuration file, which is available from the Google Developer Console. To create the...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android 9 Development Cookbook - Third Edition
Published in: Oct 2018Publisher: PacktISBN-13: 9781788991216
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Rick Boyer

Rick Boyer has been programming professionally for over 20 years. He has written apps on Windows, created websites, and coded for various mobile devices, including Windows CE, Windows Phone, and Android. Almost eight years ago, he took the plunge and started his own software consulting business, NightSky Development, focusing exclusively on Android development.
Read more about Rick Boyer