Android Intents and Services Lab 3 PDF
Document Details
Uploaded by IdyllicEpiphany1838
Arba Minch University
Tags
Summary
This document provides an introduction to Android Intents and Services, including examples of explicit and implicit intents, services for background tasks, and the Android Activity Lifecycle. It covers topics like creating and using Intents for different actions. A variety of code examples and diagrams are included to enhance understanding. This document appears to be part of a lab session aimed at teaching Android application development.
Full Transcript
Intents and Services Lab session Outline Intent ○ Explicit - Login and Home screen example ○ Implicit – A URL opener activity example Service A background music player example Intents Code Example Login-Home Example – Information We will assume correct us...
Intents and Services Lab session Outline Intent ○ Explicit - Login and Home screen example ○ Implicit – A URL opener activity example Service A background music player example Intents Code Example Login-Home Example – Information We will assume correct username and password (android,admin) Create two Activities in a single project ○ Login Activity Two EditText One Button ○ Home Activity Displays a welcome message with the name of the user logged in The Login Activity CONTAINS ○ 1 TextView ○ 2 EditText ○ 1 Button Clicking the Login button will start the Home Activity The Home Activity CONTAINS ○ 2 TextView First text view displays Home page title Second text view displays welcome message with the name of the user who has logged in Login Activity roughly The Login Activity methods in detail The onCreate() method onClick() method Home Activity Implicit Intents – List of Actions ACTION_VIEW: Display data to the user. The data can be a file, a URL, or any other type of data that can be viewed. ACTION_EDIT: Edit or modify existing data. ACTION_SEND: Send data to another app. This action is commonly used for sharing content. ACTION_DIAL: Initiate a phone call to the specified phone number. ACTION_CALL: Call the specified phone number without showing a dialer screen. Requires the CALL_PHONE permission. ACTION_SENDTO: Send a message to a specific recipient. ACTION_WEB_SEARCH: Perform a web search using the specified query. Implicit Intents – List of Actions ACTION_PICK: Pick an item or data from a list. ACTION_GET_CONTENT: Get content from another app or from the system. ACTION_IMAGE_CAPTURE: Open the device's camera app to capture an image. ACTION_VIDEO_CAPTURE: Open the device's camera app to record a video. ACTION_SETTINGS: Open the system settings screen. ACTION_OPEN_DOCUMENT: Open a document or file picker to select a file. ACTION_INSTALL_PACKAGE: Install an app package. Requires the REQUEST_INSTALL_PACKAGES permission. ACTION_DELETE: Delete data or a file. A URL Opener Example - Information Create an Intent with a VIEW action Intent intent =new Intent(Intent.ACTION.VIEW,destination) The above statement contains 2 parameters ○ The action ○ The destination ( Uri destination=Uri.parse(“url”) ) Clicking the go button will open the url with the selected application The MainActivity layout CONTAINS ○ 1 Edit text ○ 1 Button Clicking the go button will open the entered URL The MainActivity Service Example Music player service - Information Include the music(mp3) file in your project directory first ○ Right click on app folder ○ New ○ Android resource directory ○ Change resource type to raw ○ Ok Now copy and paste your mp3 file to the raw folder Create a MusicPlayer object in your java file MediaPlayer player; Create a separate service class– see next slide Create the Activity With two buttons Using any layout Play button will start a service Stop button will stop a service The MainActivity class roughly The MainActivity methods in Detail onCreate() method The onClick() method Rough view of the Service Class Use the MediaPlayer object to create the player Player= MediaPlayer.create(this, R.raw.file) It requires two parameters ○ The context ○ The file name of the music file without file extension from R.raw directory Player.start() will play the music Player.pause() will pause the music Player.stop() will stop the music Player.setLooping(Boolean) will continue to play the music if true. The Service methods in detail Ibinder() method onStartCommand() method onDestroy() method Finally, Check the AndroidManifest.xml file for Service The onStartCommand( ) Called multiple times based on a call to startService() Takes three parameters ○ Intent ○ Int Flag – flag for start request ○ Int StartID – ID for the start request Returns an int constant which can be: ○ START_STICKY – will restart your service after getting killed by system. ○ START_NOT_STICKY – means you don’t need your service to be restarted after getting killed by the system. ○ START_REDELIVER_INTENT –similar to START_STICKY but in this the intent used to start the service will be redelivered Done; You can connect a physical device and test the app. Homework Modify the Media Player service to get an audio file from a file and play the chosen audio file. [See screen]