Android Training Lesson 7: FPT Software

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

FPT Software

ANDROID TRAINING
LESSON 7
Version 0.1

© Copyright 2011 FPT Software 1


Agenda

• Content Provider Basics


• Accessing a Content Provider
• Developing a Custom Content Provider

2/11/2012 Android Insights - 3 2/18


© Copyright 2011 FPT Software 2
Application Building Blocks

© Copyright 2011 FPT Software 3


Android Application Anatomy

Activities Services
1.Provides User Interface 1. No User Interface
2.Usually represents a Single Screen 2.Runs in Background
3.Can contain one/more Views 3. Extends the Service Base Class
4. Extends the Activity Base class

Application= Set of Android Components

Intent/Broadcast Receiver Content Provider


1.Receives and Reacts to broadcast 1.Makes application data available
Intents to other apps
2.No UI but can start an Activity 2.Data stored in SQLite database
3. Extends the ContentProvider
3. Extends the BroadcastReceiver
Base class
Base Class
© Copyright 2011 FPT Software 4
Broadcast Receivers
1. A broadcast receiver is a component that responds to system-
wide Broadcast announcements.
2. A broadcast receiver is registered as a receiver in an Android
Application via the AndroidManifest.xml file, or You can also
register BroadcastReceiver dynamically via the
Context.registerReceiver() method.
3. Many broadcasts originate from the Android system—for
example, a Broadcast announcing that the screen has turned off,
the battery is low, or a picture was captured or an SMS is
received.

© Copyright 2011 FPT Software 5


Broadcast Receivers

• A broadcast receiver will be able to receive intents


which can be generated via the
Context.sendBroadcast() method.
• The class BroadcastReceiver defines the onReceive()
method. Only during this method your
BroadcastReceiver object will be valid, afterwards
the Android system can recycle the
BroadcastReceiver. Therefore you cannot perform
any asynchronous operation in the onReceive()
method. .

© Copyright 2011 FPT Software 6


Pending Intent

•  A PendingIntent is a token that you give to another


application (e.g. Notification Manager, Alarm
Manager or other 3rd party applications), which
allows this other application to use the permissions
of your application to execute a predefined piece of
code.
• To perform a broadcast via a pending intent so get a
PendingIntent via PendingIntent.getBroadcast().
• To perform an activity via an pending intent you
receive the activity via PendingIntent.getActivity().

© Copyright 2011 FPT Software 7


Pending Intent

• There are three static PendingIntentfactory methods which can be used to obtain a
PendingIntent
– public static PendingIntent getActivity(Context context, int requestCode, Intent intent,
int flags)
– public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent,
int flags)
– public static PendingIntent getService(Context context, int requestCode, Intent intent,
int flags)
• These return PendingIntent instances which can be used to
– start an activity,
– perform a broadcast, or
– start a service
• Depending upon the given arguments each of these methods can either
– create a new PendingIntent object,
– modify an existing PendingIntent object,
– modify an existing PendingIntent object and create a new PendingIntent object, or
– do nothing

© Copyright 2011 FPT Software 8


Broadcast Receivers
1. We’ll use a Broadcast Receiver to capture SMS receive event
2. We capture the SMS receive event and launch an Activity to show the sms and give user
an option to reply the SMS

Activity

OS BroadcastReceiver

© Copyright 2011 FPT Software 9


Broadcast Receivers
1. Create a new project BroadcastReceiverDemo
2. A broadcast receiver is implemented as a subclass of BroadcastReceiver and each
broadcast is delivered as an Intent object. In this case the intent is detected by
android.provider.Telephony.SMS_RECEIVED

To do this we’ll create a class SMSReceiver that extends BroadcastReceiver class


and define the method onReceive()

BroadcastReceiver

© Copyright 2011 FPT Software 10


Broadcast Receivers
3. We also need to add SMSReceiver as receiver of a particular Intent (SMS received)
which is identified by android.provider.Telephony.SMS_RECEIVED

BroadcastReceiver

© Copyright 2011 FPT Software 11


Broadcast Receivers
4. Also we have to add permission for receiving SMS

BroadcastReceiver

© Copyright 2011 FPT Software 12


Broadcast Receivers
5. Now we run the application
6. Now we use emulator control to send sms

© Copyright 2011 FPT Software 13


Receiving SMS
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
String address="";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
address=msgs[i].getOriginatingAddress();
Toast.makeText(context, str, Toast.LENGTH_LONG).show();

© Copyright 2011 FPT Software 14


Sending SMS
1. Add permission in menifest.xml

2. We add the following code for sending SMS from anywhere of our application

© Copyright 2011 FPT Software 15


Services

• it runs in the same process as the application


it is part of.
• To create a application to run in the
background of other current activities, one
needs to create a Service. The Service can run
indefinitely (unbounded) or can run at the
lifespan of the calling activity(bounded).

© Copyright 2011 FPT Software 16


Services

© Copyright 2011 FPT Software 17


Services

• a Service has a different lifecycle than activities therefore have different


methods. But to begin a service in the application a call to startService()
which envokes the service onCreate() method and onStart() beginning
running the service.
context.startService() | ->onCreate() - >onStartCommand() [service
running]
Calling the applications stopService() method to stop the service.
context.stopService() | ->onDestroy() [service stops]
context.onBindService() | ->onCreate() [service created] . The client will
receive the IBinder object that the service returns from its onBind(Intent)
method, allowing the client to then make calls back to the service.

© Copyright 2011 FPT Software 18


Homework

• Create SMS application:


– Display incoming phone number
– Quick reply
– close

© Copyright 2011 FPT Software 19


Homework

© Copyright 2011 FPT Software 20


Thank you!

© Copyright 2011 FPT Software 21

You might also like