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 13. Telephony, Networks, and the Web

In this chapter, we will cover the following topics:

  • How to make a phone call
  • Monitoring phone call events
  • How to send SMS (text) messages
  • Receiving SMS messages
  • Displaying a web page in your application
  • Checking online status and connection type
  • Phone number blocking API

Introduction


We'll start this chapter by looking at telephony functionality with How to make a phone call. After exploring how to make a call, we'll look at how to monitor a phone call with monitoring phone call events. We'll move on to SMS messaging in the How to send SMS messages section, and then we'll cover receiving SMS messages in the Receiving SMS messages section.

We'll then explore WebView for adding browser functionality to your app. At its basic level, WebView is a basic HTML viewer. We'll show how you can extend a WebViewClient class and modify the settings through WebSettings to create full browser functionality, including JavaScript and Zoom features.

The last recipe of this chapter will explore a new API (added in Android 7.0 Nougat) for blocking phone numbers at the OS level.  

How to make a phone call


As we've seen in previous recipes, we can call the default applications simply by using an Intent. There are two Intents for phone calls:

  • ACTION_DIAL: Uses the default phone application to make the phone call (no permission required)
  • CALL_PHONE: Bypasses the UI to directly dial the number (requires permission)

Here's the code to set and call the Intent for using the default Phone app:

Intent intent = new Intent(Intent.ACTION_DIAL); 
intent.setData(Uri.parse("tel:" + number)); 
startActivity(intent); 

Since your application is not doing the dialing and the user must press the Dial button, your app does not need any dialing permissions. The recipe that follows will show you how to place a call directly, bypassing the Dialer app.  

Getting ready

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

How to do it...

First, we need to add the appropriate permission to make...

Monitoring phone call events


In the previous recipe, we demonstrated how to make a phone call, both with an Intent to call the default application, as well as by directly dialing the number with no UI.

What if you want to be notified when the calls ends? This is where it gets a bit more complicated, as you'll need to monitor the Telephony events and track the phone state. In this recipe, we'll demonstrate how to create a PhoneStateListener to read phone state events.

Getting ready

Create a new project in Android Studio and call it PhoneStateListenerUse the default Phone & Tablet optionsand select Empty Activity in the Add an Activity to Mobile dialog.

Although it's not required, you can use the previous recipe to initiate a phone call. Otherwise, use the default dialer and/or watch the events from an incoming call. 

How to do it...

We only need a single TextView on the layout to display the event information. Open the activity_main.xml file and follow these steps:

  1. Add or modify the TextView...

How to send SMS (text) messages


Since you're probably already familiar with SMS (or text) messages, we won't spend time explaining what they are or why they are important. (If you're not familiar with SMS or want more information, see the link provided in the See also section of this recipe.) This recipe will demonstrate how to send an SMS message. (The next recipe will demonstrate how to receive notifications of new messages and how to read existing messages.)

Getting ready

Create a new project in Android Studio and call it SendSMSUse the default Phone & Tablet options and select Empty Activity in the Add an Activityto Mobile dialog.

How to do it...

First, we need to add the necessary permissions for sending an SMS. Then, we'll create a layout with phone number and message fields and a Send button. When the Send button is clicked, we'll create and send the SMS. Here are the steps:

  1. Open the Android Manifest and add the following permission:
<uses-permission android:name="android.permission...

Receiving SMS messages


This recipe will demonstrate how to set up a broadcast receiver to notify you of new SMS messages. It's useful to note that your app does not need to be running to receive the SMS Intent. Android will start your service to process the SMS.

Getting ready

Create a new project in Android Studio and call it ReceiveSMSUse the default Phone & Tablet options and select Empty Activity in the Add an Activityto Mobile dialog.

How to do it...

We won't be using a layout in this demonstration as all the work will be in the Broadcast Receiver. We'll use Toasts to display incoming SMS messages. Open the Android Manifest and follow these steps:

  1. Add the following permission:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
  1. Add the following declaration for the broadcast receiver to the application element:
<receiver android:name=".SMSBroadcastReceiver">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>...

Displaying a web page in your application


When you want to show a web page, you have two choices: call the default browser or display the content in your app. If you just want to call the default browser, use an Intent as follows:

Uri uri = Uri.parse("https://www.packtpub.com/"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(intent); 

If you need to display the content in your own application, you can use WebView. This recipe will show how to display a web page in your application, as can be seen in this screenshot:

Getting ready

Create a new project in Android Studio and call it WebViewUse the default Phone & Tablet options and select Empty Activity in the Add an Activityto Mobile dialog.

How to do it...

We're going to create the WebViewthrough code so we won't be modifying the layout. We'll start by opening the Android Manifest and following these steps:

  1. Add the following permission:
<uses-permission android:name="android.permission.INTERNET"/>
  1. Modify the existing...

Checking online status and connection type


This is a simple recipe, but one that is very common and will probably be included in every internet application you build: checking online status. While checking online status, we can also check the connection type: WIFI or MOBILE.

Getting ready

Create a new project in Android Studio and call it isOnlineUse the default Phone & Tablet options and select Empty Activity in the Add an Activityto Mobile dialog.

How to do it...

First, we need to add the necessary permissions to access the network. Then, we'll create a simple layout with Button and TextView. To get started, open the Android Manifest and follow these steps:

  1. Add the following permissions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  1. Open the activity_main.xml file and replace the existing layout with the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android...

Phone number blocking API


A new feature introduced in Android Nougat (API 24) is the ability to handle blocking phone numbers at the OS level. This provides a consistent experience for the user across multiple devices with the following:

  • Blocked Numbers block both incoming calls and text messages
  • Blocked Numbers can be backed up using the Backup & Restore feature
  • All apps on the device share the same Blocked Numbers list

In this recipe, we will look at the code to add a number to block, remove the number, and how to check whether the number is already blocked.

Getting ready

Create a new project in Android Studio and call it BlockedCallList. In the Target Android Devices dialog, select the Phone & Tablet option and choose API 24: Android 7.0 Nougat (or higher) for the Minimum SDK. Select Empty Activity in the Add an Activity to Mobile dialog.

How to do it...

We will start by creating a UI with an EditText to enter a phone number and three buttons: Block, Unblock, and isBlocked. To start,...

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