android
COIT20270 Application Development for Mobile Platforms Week 9: Developing Android Services and App testing
Dr. R. Balsys, CQU, 2018.
Source: Beginning Android Programming with Android Studio, J.F. DiMarzio, 2016 and https://www.tutorialspoint.com/mobile_testing/mobile_testing_quick_guide.htm
Week 9 – Developing Services
Objectives, to:
To create an Android service that runs in the background
Perform long running tasks in threads
Perform repeated tasks in a service
Understand how activities and services communicate
Understand threading in Android
2
CQU - COIT20270 Application Development for Mobile Platforms
Android Services
Services are tasks that run in the background without the need for a UI, e.g playing background music or repeatedly polling for the devices current location
To create a service you extend the Service base class and override:
onBind(): binds the activity to the service
onStart(): used to do things when the service first starts
onDestroy(): called when the service terminates and is used to clean up resources used by the service
3
CQU - COIT20270 Application Development for Mobile Platforms
Go to the API and read through the entries
3
…Android Services
Services must be declared using the service tag, or an action tag in your intent-filter section for services that are available to other applications, in the Android manifest file
You start the service using the startServices() method
You stop a service by calling the stopService() method
4
CQU - COIT20270 Application Development for Mobile Platforms
Go to the API and read through the entries
4
Performing Long Running Tasks
Long running tasks will stall your UI making your activity unresponsive
For this reason long running tasks are put into a separate thread so the main thread can keep running
You implement an inner class that extends the asynchronous AsyncTask class without manually creating threads and handlers
5
CQU - COIT20270 Application Development for Mobile Platforms
Go to the API and read through the entries
5
The AsyncTask Class
The AsyncTask class has three generic types URL, Integer and Long
These type of parameters are passed to the doInBackground(), onProgressUpdate() and onPostExecute() methods of the AsyncTask class
doInBackground() accepts an array of String’s corresponding to URL’s as its parameter. In this method you implement the long running task. To report progress you call publishProgress() method passing the Integer % complete
onProgressUpdate() connects to UI’s elements to show tasks percentage complete
onPostExecute() takes the Long value returned by doInBackground() when it completes as its parameter and is used to take action when this happens
Call stopSelf() in onPostExecute() to terminate the Service or it keeps running
6
CQU - COIT20270 Application Development for Mobile Platforms
new Media is correct capitalisation for the constructor
6
Doing Repeated Tasks in a Service
You may want to use the Timer class to arrange for a service to be run at set time intervals
The TimerTask class implements a Runnable interface and runs on its own thread. Hence it does not have to extend the AsyncTask class to be run in the background
7
CQU - COIT20270 Application Development for Mobile Platforms
7
…Doing Repeated Tasks in a Service
To repeat the service in the class create a method in your Service and in it use a Timer object and call its scheduleAtFixedRate() method passing in a new TimerTask() that implements a run() method as scheduleAtFixedRate()’s 1st parameter, the delay before the 1st call of the run() method as the 2nd parameter and the delay between successive calls in milli-seconds as the 3rd parameter
Call your new method directly from the onStartCommand() method of your Service
8
CQU - COIT20270 Application Development for Mobile Platforms
8
Asynchronous Tasks on Separate Threads Using IntentService
The IntentService class makes it easy to create an asynchronous task that terminates when completed
IntentService is based on the Service class and handles asynchronous requests on demand
It is started like a normal service, executes on a separate worker thread and terminates itself when completed
9
CQU - COIT20270 Application Development for Mobile Platforms
9
…Asynchronous Tasks on Separate Threads Using IntentService
To use IntentService class you define a new class that extends IntentService
You need to implement a constructor that calls the superclass passing in a string to name the service
You also need to override the onHandleIntent() method to specify the action the service is to take when executing on the worker thread
When onHandleIntent() is finished the thread is terminated and then service is automatically stopped
10
CQU - COIT20270 Application Development for Mobile Platforms
10
Week 9 – Mobile App Testing
This week we:
Understand the importance of testing the the apps we develop for mobile devices
Develop a methodology for testing mobile devices
Understand how to choose which mobile devices we should test
Discuss strategies for testing on a range of devices (including emulators)
Look at developing a mobile device test plan
11
CQU - COIT20270 Application Development for Mobile Platforms
The Importance of App testing
There is a lot of competition for application and services on mobile devices
Testing ensures that your device functions as intended
If your app fails at some task, is poorly designed or is unresponsive your customers will move on to something else very quickly
The goal of testing is to determine the quality of the software – does it work, does it function as intended and does it meet the users needs
Failure means your customers will desert you for other suppliers
12
CQU - COIT20270 Application Development for Mobile Platforms
Mobile Testing Methodology
Comprehensive testing is complicated and expensive
In Mobile apps use is often made of the WWW (even though it does not have too) so account must be taken of this
Testing must not just be restricted to the mobile device but any services or peripherals the app uses
13
CQU - COIT20270 Application Development for Mobile Platforms
Note: The reading addresses testing mobile web sites rather than mobile app testing. However, a lot of similarity occurs between the two
13
…Mobile Testing Methodology
According to Wikipedia mobile application testing includes
Functional testing
Laboratory testing – of complete wireless network
Performance testing – undertaken under different conditions
Memory leakage – test apps memory management
Interrupt testing – how it handles interrupts such as battery removal, notifications, incoming/outgoing calls, etc..
Usability testing
Installation testing in actual practice
Any certification testing required by customers
14
CQU - COIT20270 Application Development for Mobile Platforms
Note: The reading addresses testing mobile web sites rather than mobile app testing. However, a lot of similarity occurs between the two
14
Choosing Devices to Test
Comprehensive testing targets multiple device models, operating systems, mobile networks and possible browsers
It generally is not possible to test on every mobile device so a reasonable subset must be chosen
The process to follow includes;
List all target devices
Organise devices by operating system and version
Organise devices by modality and input method
Choose the strongest and weakest from the list for testing
15
CQU - COIT20270 Application Development for Mobile Platforms
Note: The reading addresses testing mobile web sites rather than mobile app testing. However, a lot of similarity occurs between the two
15
Strategies for Testing on Devices
The greatest issue is to acquire the range of devices needed for testing
This can be done by
Purchasing the devices outright
Joining industry developer programs
Use the services of a commercial provider to test
Use device emulators for testing
16
CQU - COIT20270 Application Development for Mobile Platforms
Note: The reading addresses testing mobile web sites rather than mobile app testing. However, a lot of similarity occurs between the two
16
Developer Programs and Emulators
All the major manufactures of mobile devices have developer programs that you can join for a fee that may provide discounted device purchases, you to rent or borrow devices and/or access to virtual devices
Emulators are a relatively inexpensive alternate to real devices, however these are not the real devices and problems or errors can slip through the testing process
In particular gestures and touches do not emulate well
17
CQU - COIT20270 Application Development for Mobile Platforms
Note: The reading addresses testing mobile web sites rather than mobile app testing. However, a lot of similarity occurs between the two
17
Develop a Mobile Test Plan
Start by writing a summary to describe the purpose of the app, its use cases and how you plan to test the device
Review the list of devices, OS versions etc. you plan to test
Decide whether testing uses actual devices, emulators or both. If using actual devices decide how the devices are to be acquired
Write scripts to detail the tests to be undertaken on the devices and carry out the tests
18
CQU - COIT20270 Application Development for Mobile Platforms
Note: The reading addresses testing mobile web sites rather than mobile app testing. However, a lot of similarity occurs between the two
18