1. Before you begin
This codelab introduces a new app called Water Me that you'll build on your own. This codelab walks you through the steps to complete the Water Me app project, including project setup and testing within Android Studio.
Prerequisites
- This project is for students who have completed Unit 6 of the Android Basics in Kotlin course.
What you'll build
- Schedule a notification using a custom
Worker
in an existing app.
What you'll need
- A computer with Android Studio installed.
2. Finished app overview
The Water Me! app consists of a list of plants, some information about them, and a description for how often each one should be watered. For each of these plants, the completed app will schedule a reminder for when they should be watered.
Reminders will be displayed as notifications on the device, even if the Water Me! app is not running. Tapping a notification launches the Water Me! app.
For this functionality to work, your task is to schedule a background task using a custom Worker
that displays the notification.
3. Get started
Download the project code
Note that the folder name is android-basics-kotlin-water-me-app
. Select this folder when you open the project in Android Studio.
To get the code for this codelab and open it in Android Studio, do the following.
Get the code
- Click on the provided URL. This opens the GitHub page for the project in a browser.
- On the GitHub page for the project, click the Code button, which brings up a dialog.
- In the dialog, click the Download ZIP button to save the project to your computer. Wait for the download to complete.
- Locate the file on your computer (likely in the Downloads folder).
- Double-click the ZIP file to unpack it. This creates a new folder that contains the project files.
Open the project in Android Studio
- Start Android Studio.
- In the Welcome to Android Studio window, click Open an existing Android Studio project.
Note: If Android Studio is already open, instead, select the File > New > Import Project menu option.
- In the Import Project dialog, navigate to where the unzipped project folder is located (likely in your Downloads folder).
- Double-click on that project folder.
- Wait for Android Studio to open the project.
- Click the Run button
to build and run the app. Make sure it builds as expected.
- Browse the project files in the Project tool window to see how the app is set-up.
4. Schedule notification using WorkManager
All the functionality for the Water Me! app is already implemented, except for the part to schedule and a notification. The code for displaying a notification is in WaterReminderWorker.kt
(in the worker package). This happens in the doWork()
method of a custom Worker
class. Because notifications may be a new topic, this code is already implemented.
override fun doWork(): Result {
val intent = Intent(applicationContext, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent
.getActivity(applicationContext, 0, intent, 0)
val plantName = inputData.getString(nameKey)
val builder = NotificationCompat.Builder(applicationContext, BaseApplication.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_android_black_24dp)
.setContentTitle("Water me!")
.setContentText("It's time to water your $plantName")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
with(NotificationManagerCompat.from(applicationContext)) {
notify(notificationId, builder.build())
}
return Result.success()
}
Your task is to create a OneTimeWorkRequest
that will call this method with the correct parameters from PlantViewModel
.
Create work requests.
To schedule the notification, you'll need to implement the scheduleReminder()
method in PlantViewModel.kt
.
- Create a variable called
data
usingData.Builder
. The data should consist of a single string value whereWaterReminder.Worker.nameKey
is the key and theplantName
that was passed intoscheduleReminder()
is the value. - Create a one-time work request using
WaterReminderWorker
, using thedelay
andunit
passed into thescheduleReminder()
function, and setting the input data to thedata
variable you created. - Call the
workManager
'senqueueUniqueWork()
method, passing in the plant name, usingREPLACE
as theExistingWorkPolicy
, and the work request.
Your app should now be working as expected. Since each reminder will take a long time to appear, we recommend running the included tests to verify that the notification works as expected.
5. Testing instructions
Running your tests
To run your tests, you can do one of the following.
For a single test case, open up a test case class and click the green arrow to the left of the class declaration. You can then select the Run option from the menu. This will run all of the tests in the test case.
Often you'll only want to run a single test, for example, if there's only one failing test and the other tests pass. You can run a single test just as you would the entire test case. Use the green arrow and select the Run option.