Instruction

Getting Started

Before you start implementing networking in the app, you must learn and do several things.

Setting Up Permissions

By default, Android doesn’t let apps send or receive data from the internet. To perform network operations, you must declare permissions for it in the AndroidManifest.xml file. The most important permissions are:

  • <uses-permission android:name="android.permission.INTERNET"/>: This lets the app access the internet.
  • <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>: This enables the app to check the network’s connectivity status.

Both of these permissions are considered normal. That means you don’t need to request them at runtime. They’re granted at install time.

There’s also the <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> permission that grants your app access to Wi-Fi network information and configuration. But you won’t need it for this course.

Separating Networking Code

When adding networking code to the project, it’s a good idea to separate it from the rest of your app. This makes it easier to maintain and test the code. You’ll use the MovieDiaryApi.kt file in the sample app to implement your networking code and separate it from the UI.

Real-world apps usually have multiple data sources such as a remote API, local database, shared prefs, file system, or any combination of those. That’s why you’ll most likely see the networking code hidden behind repositories. Those are classes or interfaces that abstract the data source away from the caller. Because our sample app has only one data source — the remote API — you won’t use repositories in this course.

You’ll take one more shortcut. Usually, you won’t make API calls directly from your UI code. Rather, you’ll implement a ViewModel instance that handles API calls and prepares the UI state when a response returns. To reduce complexity and focus on the subject, the sample project doesn’t use ViewModels.

In the next section, you’ll review the project structure, add the necessary permissions to the AndroidManifest.xml, and create a class to check the network status.

Opening the Project

Find the code for this course in the student materials repository by clicking the GitHub icon next to the content. Download the materials and open the Starter project for Lesson 1 in Android Studio version Hedgehog or newer. Before you start adding code, open Android Studio Settings and navigate to EditorGeneralAuto Import. Make sure the option Add unambiguous imports on the fly is checked.

Selecting Add unambiguous imports on the fly option
Selecting Add unambiguous imports on the fly option

When you’re done, proceed to the demo section where you’ll learn more about the project structure, and implement functionality to check network status in your app.

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo 1