Chapter Two: Part 3
Working with background services
Mulugeta G.
1
Introduction to Intents
• An Intent is a simple message object that is used to
communicate between android components such as
activities, content providers, broadcast receivers
and services.
• Intents are also used to transfer data between
activities.
• Intents are used generally for starting a new activity
using startActivity().
2
Intent
• Intent is used to invoke components. It is mainly
used to:
– Start the service
– Launch an activity
– Display a web page
– Display a list of contacts
– Broadcast a message
– Dial a phone call etc.
3
Use of Intent
• For Launching an Activity
• To start a New Service
• For Broadcasting Messages
• To Display a list of contacts in ListView
4
Types of Intent in Android
• Two types of Intent:
– Implicit Intent
– Explicit Intent
1. Implicit Intent
• specifies an action that can invoke any app on the
device to be able to perform an action.
• is useful when your app cannot perform the action
but other apps probably can and you’d like the user
to pick which app to use.
5
2. Explicit Intent
• is an Intent where you explicitly define the
component that needs to be called by the Android
System.
• is one that you can use to launch a specific app
component, such as a particular activity or service
in your app.
6
• The Different Methods Used in Intent
– Action_Main: Adds an action to an intent Filter.
– Action_Pick: It is using for picking the image from CAMERA or
GALLERY
– Action_Chooser: It is used for choosing the image from the gallery
– Action_Dial: Display the phone dialer with the given number filled in.
– Action_Call: Placing and immediate phone call
– Action_Send: Sending Text content from one activity to other.
– Action_SendTo: Preparing an SMS. The text is supplied as an Extra
element. The intent excepts such as values to be called “sms_body”
7
Intent select = new Intent(Intent.ACTION_CAMERA);
send.setData(uri);
startActivity(Intent.createChooser(
select,”Select an Image from Camera”));
8
9
Launching Activities and Sub Activities
An activity represents a single screen with a user
interface just like window or frame of Java.
Android system initiates its program with in
an Activity starting with a call
on onCreate() callback method.
There is a sequence of callback methods that start
up an activity and a sequence of callback methods
that tear down an activity as shown in the below
Activity life cycle diagram.
10
11
Sr.No Callback & Description
onCreate()
1 This is the first callback and called when the activity is first created.
onStart()
2 This callback is called when the activity becomes visible to the user.
onResume()
3 This is called when the user starts interacting with the application.
onPause()
The paused activity does not receive user input and cannot execute any
4 code and called when the current activity is being paused and the
previous activity is being resumed.
onStop()
5 This callback is called when the activity is no longer visible.
onDestroy()
6 This callback is called before the activity is destroyed by the system.
onRestart()
7 This callback is called when the activity restarts after stopping it.
12
WebView
• WebView is a view that display web pages inside
your application.
• In order to add WebView to your application, you
have to add <WebView>element to your xml layout
file. Its syntax is as follows −
13
• In order to use it, you have to get a reference of this
view in Java file.
• To get a reference, create an object of the class
WebView. Its syntax is:
WebView browser = (WebView) findViewById(R.id.webview);
• In order to load a web url into the WebView, you
need to call a method loadUrl(String url) of the
WebView class, specifying the required url.
• Its syntax is:
browser.loadUrl("http://www.ebc.et/");
14
Sr.No Method & Description
1 canGoBack()
This method specifies the WebView has a back history item.
2 canGoForward()
This method specifies the WebView has a forward history item.
3 clearHistory()
This method will clear the WebView forward and backward history.
4 destroy(): This method destroy the internal state of WebView.
5 findAllAsync(String find)
This method find all instances of string and highlight them.
6 getProgress(): This method gets the progress of the current page.
7 getTitle(): This method return the title of the current page.
8 getUrl(): This method return the url of the current page.
15
• If you click on any link inside the webpage of the
WebView, that page will not be loaded inside your
WebView.
• In order to do that you need to extend your class
from WebViewClient and override its method
browser = (WebView) findViewById(R.id.webView);
WebSettings webSettings= browser.getSettings();
webSettings.setJavaScriptEnabled(true);
browser.loadUrl("http://www.ebc.et/");
browser.setWebViewClient(new WebViewClient());
16
Creating and Invoking Services
• The primary mechanism for background work in
Android is the service.
• An Android service is a component that is designed to
do some work without a user interface.
• A service might download a file, play music, or apply a
filter to an image.
• Services can also be used for interprocess
communication (IPC) between Android applications.
• For example one Android app might use the music
player service that is from another app or an app
might expose data (such as a person's contact
information) to other apps via a service. 17
Using a Content Provider
• A content provider manages access to a central
repository of data.
• A provider is part of an Android application, which
often provides its own UI for working with the data.
• However, content providers are primarily intended
to be used by other applications, which access the
provider using a provider client object.
• Together, providers and provider clients offer a
consistent, standard interface to data that also
handles inter-process communication and secure
data access.
18
• A content provider coordinates access to the data
storage layer in your application for a number of
different APIs and components , these include:
– Sharing access to your application data with other
applications
– Sending data to a widget
– Returning custom search suggestions for your application
through the search framework using
SearchRecentSuggestionsProvider
– Synchronizing application data with your server using an
implementation ofAbstractThreadedSyncAdapter
– Loading data in your UI using a CursorLoader
19
20
Requesting and Requiring Permission
• Every Android app runs in a limited-access sandbox.
• If an app needs to use resources or information
outside of its own sandbox, the app has to request the
appropriate permission.
• You declare that in AndroidManifest.xml
– put a <uses-permission> element in your app manifest, as
a child of the top-level <manifest> element.
21
Packaging an Application(Deployment)
• In Android Studio, select Build > Build APK in the
menu bar.
• The APKs are saved
in projects/Polyfills/cordova/platforms/android/buil
d/outputs/apk/
• Note: Before you can generate a release version of
your app for public distribution, you must
sign your APK
22
Installing Application on Android Device
• We have already seen how to create .apk file and
how to send into mobile device
• Finally, how we can install the .apk file
23
Publishing on the Android Market
• Publishing is the general process that makes your
Android applications available to users.
• When you publish an Android application you perform
two main tasks:
1. You prepare the application for release.
– During the preparation step you build a release version of
your application, which users can download and install on
their Android-powered devices.
2. You release the application to users.
– During the release step you publicize, sell, and distribute the
release version of your application to users.
• For more information visit:
– https://developer.android.com/studio/publish/ 24
Tips: Android Color
25
Tips: Android Color
26
Tips: Android Color
27
Tips: Android Color
28
Thank You
?
29