/
Intent Intent

Intent - PowerPoint Presentation

calandra-battersby
calandra-battersby . @calandra-battersby
Follow
399 views
Uploaded On 2016-05-15

Intent - PPT Presentation

A n Intent describes the operation to be performed Intents are used to start an activity using either of the following methods ContextstartActivity or ActivitystartActivityForResult ID: 320186

android intent activity action intent android action activity category implicit component filter data intents system target components application filters

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Intent" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Intent

A

n

Intent describes the operation to be performed. Intents are used to start an activity using either of the following methods –

Context.startActivity

() or

Activity.startActivityForResult

()

Additionally, Intents are used to interact with services and broadcast

receivers. From

an application perspective, the following diagram shows how Intents interact with activities, services and broadcast

receivers.Slide2

An intent is an Intent object with a message content.

Activities, services and broadcast receivers are started by intents.

An activity is started by Context.startActivity(Intent intent) or Activity.startActivityForResult(Intent intent, int RequestCode)A service is started by Context.startService(Intent service)An application can initiate a broadcast by using an Intent in any of Context.sendBroadcast(Intent intent), Context.sendOrderedBroadcast(), and Context.sendStickyBroadcast() Slide3

What

are Intent Objects?

An Intent object is collection (bundle) of information that needs to be passed to an activity. It typically contains*name of the activity that should handle the intent, (e.g. Module2Activity in our earlier example) and the package in which that component resides e.g. com.learncomputer*Intent action which describes what operation needs to be done. For activities, the possible intent actions include the following:ACTION_CALL – make a phone callACTION_EDIT – show data on screen for user editing

ACTION_MAIN – start the main activity

ACTION_SYNC – synchronize device data with server

*Data

which is the information required to complete the operation described above. It would typically be a phone number (for calling), displaying text data (for user editing) etc.

*Category

which helps to identify the target activity for intent. Typical categories include CATEGORY_HOME (return user to home screen), CATEGORY_PREFERENCES (open the preference panel), CATEGORY_BROWSABLE (invoke browser to open a web page, render an image or load an email message) etc.

*Extras

which provide a means to pass additional info in the form of key-value pairs

Flags determining the various aspects of target activity e.g. whether it should be added to the list of recent activities.Slide4

Types

of Intents

Intents can be implicit and explicit. Explicit intents are identifiable by name and these are typically used by activities for internal messaging, e.g. to start another activity.On the other hand, implicit intents are handled by the Android system and it in turn chooses the best suited activity to perform the required action as stated in the intent. Slide5

Intent Filters

Intent

Filters are used with implicit intents. Activities which want to handle implicit intents will associate themselves with Intent Filters. As the name itself suggests, an Intent Filter essentially signifies what types of intents an activity would like to receive and which ones it would like to filter.Creating Intent FiltersIntent Filters are defined in the manifest file using the <intent-filter> tag. An Intent Filter has fields which map one to one with the action, data, and category fields of an Intent object. For intent to be delivered to an implicit activity, it must pass all three criteria of action, data and categorySlide6

Intent

Filter and

its associated fields (action, data and category)<intent-filter> <action android:name="com.learncomputer.Module2.SHOW_CURRENT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:mimeType="audio/mpeg" android:scheme

="http" />

</intent-filter>

As per the above Intent Filter rules, the target category must act upon the SHOW_CURRENT action, must belong to the BROWSABLE category and finally use the data specification (audio/ mpeg) as per the defined scheme.

In case, intent passes through multiple filters and there are multiple target activities eligible to receive that intent, it’s developer’s responsibility to decide which activity is to be used.

An intent is an Intent object with a message content.

Activities, services and broadcast receivers are started by intents.

ContentProviders

are started by

ContentResolvers

:

An activity is started by

Context.startActivity

(Intent intent) or

Activity.startActivityForResult

(Intent

intent

,

int

RequestCode

)

A service is started by

Context.startService

(Intent service)

An application can initiate a broadcast by using an Intent in any of

Context.sendBroadcast

(Intent intent),

Context.sendOrderedBroadcast

(), and

Context.sendStickyBroadcast

() Slide7
Slide8

Intent

Intent

object is a messaging object(instance of the android.content.Intent class) which is used to signal/activate other components such as activities, services, and broadcast receivers. E.g., an activity B can register with intent A’s intent so that the bound activity B will be notified and activated when the event fires. Intent is capable to include multiple components to perform certain tasks.An Intent is sent to the Android OS system and the Android system will decide which target (for implicit intent) can do the job best. An Intent can also contain data. This data can be used by the receiving component. Slide9

Explicit intent

An explicit intent tells Android system to run specific component such as

ActivityB Intent intent = new Intent(ActivityA.this, ActivityB.class);startActivity(intent);Slide10

Implicit intent

Implicit Intents

Implicit intents specify the action which should be performed and optionally data which provides content for the action.For example, the following tells the Android system to view a webpage. All installed web browsers should be registered to the corresponding intent data via an intent filter.Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.vogella.com"));startActivity(i);

If an implicit intent is sent to the Android system, it searches for all components which are registered for the specific action and the fitting data type.

If only one component is found, Android starts this component directly. If several components are identified by the Android system, the user will get a selection dialog and can decide which component should be used for the

intentSlide11

Explicit intent vs. Implicit intent

Android Intents to trigger actions in your own apps (explicit) or some other third party app (implicit where Android resolves matching intents)Not specify the name of the target component Intent is called the "hidden-Intent. The IntentFilter Android system will find the implicit Intent object. Explicit Intent directly define the target component with the name of the component, this approach is very direct. However, developers often do not know the other components of the application name, an explicit Intent more used to deliver the message within the application.

For

example, in an application, an Activity to start a service. The implicit Intent on the contrary, it does not define the need to activate the target component, component name, it is more widely used to pass messages between different applications.

In

the

the

explicit Intent message, decided the only elements of the target component is the component name, Therefore, if you Intent has been clearly defined in the name of the target component, then you completely do not have to define the contents of the other Intent.

For

implicit Intent is different, because there is no clear target component name, must help by Android system application to find components that most closely matches the intent of the request with Intent. Specific selection method: Android Intent of the contents of the request and called

IntentFilter

filter comparison, the

IntentFilter

system contains all the possible components to be selected.

IntentFilter

matches a component implicit Intent requested content, the Android on the choice of the components as Cain Intent target component.

Android How do I know the application to handle certain types of Intent to request it? This requires the application statement in the Android-the Manifest.xml in the components contained in the filter (that can match what Intent request). A statement Intent-Filter components not only respond to his name specified explicitly Intent request, and unable to respond implicit Intent request. And a statement of the

the

IntentFilter

components either in response to the explicit Intent request, you can also respond to the implicit Intent request. Slide12

Understanding Android Intent Filters (Receiving Implicit Intents)

Intents

sends signals to the Android system telling it that some action needs to be performed by another component (activity, services, broadcast receivers) in the same app or a different app. The system starts resolving which component in which app is responsible to handle this event that just got triggered. Implicit intent that opens a webpage URL in the browser.String url = "http://google.com";Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData

(

Uri.parse

(

url

));

startActivity

(intent

);

ACTION_VIEW

intent action

and

data to google.com. Once

startActivity

() is triggered Android will start looking for all the apps that can handle this and most probably find just one which is the browser (chrome) that can receive it. It’ll launch chrome and open google.com in it.

I

f

we wanted to advertise that our app can also handle such an implicit intent where we take an HTTP URL and do some

thing.

In such a case Android will give two choices to the user – single browser installed (chrome) and our app – from which he can make a selection. Slide13

Register

a component of our app that can receive an implicit intent

Note: An explicit intent has its target specified (while creation) does not need registration.To register a component of our app that can receive an implicit intent for a specific action and data, we can declare an <intent-filter> for that particular component (activity, service or a broadcast receiver) in our manifest file.<activity android:name="com.pycitup.pyc.WebViewActivity" android:label="@string/title_activity_web_view" > <intent-filter>

<action

android:name

="

android.intent.action.VIEW

" />

<category

android:name

="

android.intent.category.DEFAULT

" />

<data

android:scheme

="http" />

</intent-filter>

</activity>

Make

sure that both the sending and receiving activities have requested permission for the type of action (ACTION_VIEW) to be performed. N

eed

the INTERNET permission in our manifest:

<uses-permission

android:name

="

android.permission.INTERNET

" />

When

the intent code

is fired, the user will be given multiple choices among which one of them will be our own app that’ll trigger

WebViewActivity

. Inside the Activity you could then do things like show up the webpage in a

WebViewSlide14

Subelement of intent filters

Each activity in the manifest can contain multiple intent filters blocks. Each intent filter specifies the type of intents it’ll accept based on the <action>, <category> and <data>

subelements. So as soon as an implicit intent passes through (or conforms to) one of the intent filters it is delivered to that activity component or at least considered in the list of initial dialog with choices.<action> – Declares the intent action that you want to be accepted by the intent filter, specified by the android:name attribute. Every <intent-filter> element must contain one or more <action> elements, else no Intent objects will get through the filter. Also note that the value must be the string value of the intent action, not the class constant (for example "android.intent.action.VIEW" and not Intent.ACTION_VIEW).<category> – Specifies the accepted intent category in the android:name

attribute. Should contain the string value rather than the class constant ("

android.intent.category.DEFAULT

" instead of

Intent.CATEGORY_DEFAULT

).

<data> – Declares the type of data accepted using various attributes that you can find here. Examples are there in the code samples above which makes this element simple to understand.

Action which is the most reasonable action to perform and Data that contains the basic information that drives our action were pretty easy to comprehend but Category is something that might need a bit of an explanation. Using the category attribute, we give/mention additional information about the action to execute (that’ll be matched for intent resolution). Slide15

CATEGORY

The

CATEGORY_DEFAULT category is applied to all implicit intents (automatically) passed to startActivity() and startActivityForResult(). So if you want your activity to receive implicit intents, it must include a category for "android.intent.category.DEFAULT" in its intent filters.CATEGORY_LAUNCHER – Show up in the Launcher (Google Experience Launcher or Google Now Launcher or Nova or some other that you installed and use) as a top-level application. In other words the activity is the initial activity of a task (will launch when app starts) and is listed in the system’s application launcher (app drawer).Slide16

An implicit intent tells Android system to run an activity(

ActivityB

in this case) that can do these things that are specified in the intent filter in a manifest Intent intent = new Intent();intent.addAction("thisAction");intent.addCategory("thisCategory");startActivity(intent); Android manifest file:<activity android:name

="

ActivityB

">

<intent-filter>

<action

android:name

="

thisAction

"/>

<category

android:name

="

thisCategory

"/>

</intent-filter>

</activity>

Android system will locate a best matched component such as

ActivityB

based on action and category requirement. Implicit intent also leaves a security vulnerability hole.Slide17

Demo

https://sites.google.com/site/mobilesecuritylabware/home/android-components/activity-intent