mobile programming pertemuan 6 presented by mulyono poltek nsc surabaya

10
Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya

Upload: jordan-houston

Post on 21-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya

Mobile ProgrammingPertemuan 6

Presented by MulyonoPoltek NSC Surabaya

Page 2: Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya

Intent

• Three of the core components of an application are:• Activities• Services• Broadcast Receivers

• They are activated through messages, called intents. Intent messaging is a facility for late runtime binding between components in the same or different applications.

• An Intent object, is a passive data structure holding an abstract description of an operation to be performed — or, often in the case of broadcasts, a description of something that has happened and is being announced.

Page 3: Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya

There are separate mechanisms for delivering intents to each type of component:

- An Intent object is passed to Context.startActivity() or Activity.startActivityForResult() to launch an activity or get an existing activity to do something new. (It can also be passed to Activity.setResult() to return information to the activity that called startActivityForResult().)

- An Intent object is passed to Context.startService() to initiate a service or deliver new instructions to an ongoing service. Similarly, an intent can be passed to Context.bindService() to establish a connection between the calling component and a target service. It can optionally initiate the service if it's not already running.

- Intent objects passed to any of the broadcast methods (such as Context.sendBroadcast(), Context.sendOrderedBroadcast(), or Context.sendStickyBroadcast()) are delivered to all interested broadcast receivers. Many kinds of broadcasts originate in system code.

- Intent are really useful with Android Service to exchange information and notification between the calling object and the Service.

Page 4: Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya

Intent Filter

• To inform the system which implicit intents they can handle, activities, services, and broadcast

• receivers can have one or more intent filters.

• Each filter describes a capability of the component, a set of intents that the component is willing to receive. It, in effect, filters in intents of a desired type, while filtering out unwanted intents — but only unwanted implicit intents (those that don't name a target class).

• An explicit intent is always delivered to its target, no matter what it contains; the filter is not consulted.

• An implicit intent is delivered to a component only if it can pass through one of the component's filters.

• An intent filter is an instance of the IntentFilter class. However, since the Android system must know about the capabilities of a component before it can launch that component, intent filters are generally not set up in Java code, but in the application's manifest file (AndroidManifest.xml) as <intent-filter> elements. (The one exception would be filters for broadcast receivers that are registered dynamically by calling Context.registerReceiver(); they are directly created as IntentFilter objects.)

Page 5: Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya
Page 6: Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya

3 fundamental use-cases

• To start an activity:

An Activity represents a single screen in an app. You can start a new instance of an Activity by passing an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data.

• To start a service:

A Service is a component that performs operations in the background without a user interface. You can start a service to perform a one-time operation (such as download a file) by passing an Intent to startService(). The Intent describes the service to start and carries any necessary data.

• To deliver a broadcast:

A broadcast is a message that any app can receive. The system delivers various broadcasts for system events, such as when the system boots up or the device starts charging. You can deliver a broadcast to other apps by passing an Intent to sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().

Page 7: Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya

Intent Types

• Explicit intents specify the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, start a new activity in response to a user action or start a service to download a file in the background.

• Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.

Page 8: Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya
Page 9: Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya

Explicit Intent

Intent downloadIntent = new Intent(this, DownloadService.class);

downloadIntent.setData(Uri.parse(fileUrl));

startService(downloadIntent);

Page 10: Mobile Programming Pertemuan 6 Presented by Mulyono Poltek NSC Surabaya

Implicit Intent

// Create the text message with a string

Intent sendIntent = new Intent();

sendIntent.setAction(Intent.ACTION_SEND);

sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);

sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // "text/plain" MIME type

// Verify that the intent will resolve to an activity

if (sendIntent.resolveActivity(getPackageManager()) != null) {

startActivity(sendIntent);

}