You are on page 1of 21

1

Mobile Computing
Intents, Intent-Filters

Lecture#06
2

Lecture Contents

Intents
Intent-Filters
Linkify
Inent-Resolution
Responding to Intents
3

Intents
Message passing mechanism used within
application or among applications:
Majorly used for
1. Declare your intention to start an activity /
service with some piece of data
2. Broadcast that an event has occurred
3. Explicitly start an activity / service
4

Full Device as Interconnected System


Enhance interaction among applications on a
device
One application can register for any global event
e.g. incoming call, sms received, sms sent, internet
connection status changed etc
Any application registered for a global event can
respond to event without caring ‘where’ that event
occurred within the system
Intents can broadcast message among system’s
applications (event driven system can be created)
5

Start Activity
Intent open = new Intent(MyActivity.this, OtherActivity.class);
startActivity(open);

1. No connection among newly opened activity and current


activity
2. For feedback from newly opened activity one should use
startActivityForResult(Inent) method instead
6

Implicit Intent
An activity can announce that some event has occurred and
some component should respond to this event (mentioning
some data for said purpose)
No need to explicitly mention which component
(activity/service) should respond to this event

Examples:
a) Call to given number
i. Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse("tel:555-2368"));
b) Open given URL in web browser
i. Intent viewIntent = new
Intent("android.intent.action.VIEW",
Uri.parse("http://www.google.com"));
c) Send SMS to provided number
7

Sub Activity & Results


Uri uri = Uri.parse("content://contacts/people");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
startActivityForResult(intent, PICK_CONTACT_SUBACTIVITY);

Bundle stats = new Bundle();


stats.putString(“channel",“Banking"");
stats.putString(“level”, “high");
stats.putString(“code", “success");
setResult(0, Activity.RESULT_OK, stats);
finish();
8

Start Implicit for Result


Uri uri = Uri.parse("content://contacts/people");
//Uri for phone contacts
Intent intent = new Intent(Intent.ACTION_PICK, uri);
//Intent just mentioning the action
startActivityForResult(intent,
PICK_CONTACT_SUBACTIVITY);
//Starting activity and waiting for result
9

Native Android Actions


ACTION_ANSWER
Handles incoming call
ACTION_CALL
Calls through default dialer
ACTION_DELETE
Lets you delete the content
ACTION_DIAL
Opens default dialer with number
ACTION_EDIT
Let you edit the data
ACTION_PICK
Lets you pick the item
10

Linkify
Automatic linking of special data with applications
TextView textView = …….;
Linkify.addLinks(textView, Linkify.WEB_URLS);

Other data types:::


1. Linkify.EMAIL_ADDRESSES
2. Linkify.WEB_URLS
3. Linkify.MAP_ADDRESSES
4. Linkify.PHONE_NUMBERS
5. Linkify.ALL
11

Linkify & XML


<TextView
Android:layout_width=“fill_parent”
Android:layout_height=“fill_parent”
Android:text=“@string/linkify_string”
Android:autoLink=“phone|email”/>
12

Intent Filter
An intent is an expression of interest for an action
to be performed (a request for something to be
happened)
Intent-Filter is an expression of interest for
response against some event or to handle some
data-type
13

Intent-Filter
Using intent-filter an application tells android that
it can serve against particular applications
coming from
1. Same application
2. Native phone applications
3. Third party applications
14

Manifest/intent-filter
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Action::::
1. Describes what action needs to be served.
2. android:name to describe name of action
3. Preferably use java package naming conventions
Category::::
4. Describes under what circumstances action should be served
5. Each intent-filter can describe multiple category tags
6. One can specify his own category values
7. Using predefined category values is also possible
15

Manifest/intent-filter
Data:::::
One can specify type of data your application can handle to
Multiple data types for one application is also possible
List of supported data types
1. android:host (valid host name e.g. com.google)
2. android:mimetype (<type
android:value=“vnd.android.Cursor.dir/*”/>)
3. android:path (valid path)
4. android:port (valid port for specified host)
5. android:scheme (requires particular scheme http:// or content://)
16

Intent-Filter
<activity>
<intent-filter>
    <action />
                <category />
                <data />
       </intent-filter>
       <meta-data />
</activity>
17

Broader Picture
Relationship among terms (Intent, Intent-Filter,
Broadcast Receiver
Intent is a wish that a job is to be done, Intent-
Filter is registration of an Activity for a
particular data type or even for some global
event (A class registering for contacts handling,
A class responding to sms received).
18

Intent-Resolution
Process of deciding which action to performed
against some event is called intent resolution. In
case of implicit intents it is very important to
understand how android resolves intents…..
Rules for intent resolution
1. All intent-filters from installed packages are kept
together
2. Intnet-filters with non-match action/category for
intent to be resolved are removed from list
3. Each part of intent’s data uri is matched with intent-
filter’s data tag
19

Intent-Resolution
4. If more than one intent-filters are matched
then highest priority value intent-filter serves
the task
5. If still conflict exists then user may be given
option to choose the application to be served
for that task
20

Response for Intent-Filter Match


When application/activity starts through an implicit
intent, it needs to find out action (what to do) and
data upon which action is to be applied
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
…….. //// Action to be done
}
21

Passing on Responsibility
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);
Intent intent = getIntent();
if(TODAY IS SATURDAY OR SUNDAY){
startNextMatchingActivity(intent);
}
}

You might also like