Sie sind auf Seite 1von 30

Lecture 5

INTENT FILTERS AND BROADCAST RECEIVERS AND MENUS

Starting Activities For Results

Starting Activities for Results


When launching an activity, some times we are expecting some results back from it. for example: The contact information of the contact that was picked The image captured from the Camera

This is done as following Starting the activity by the method startActivityForResult(Intent, int) The result will come back through your onActivityResult(int, int, Intent) method

Example : Picking Contact

Example : Picking Contact (cont.)

Handling the result


In order to successfully handle the result, you must understand what the format of the result Intent will be.

Handling results
In order to successfully handle the result, you must understand what the format of the result Intent will be.

Setting Intent results


In order to successfully handle the result, you must understand what the format of the result Intent will be.

Intent Filters

Intent Filters
For your application to serve an intent request ( may be a broadcast message) , it has to define an intent filter to tell the system that I can handle this request For example: Making your Phone Dialer app. To do this: Add an intent filter entry in the manifest file with the intent information your application can handle.

IntentFilter can match against actions, categories, and data (either via its type, scheme, and/or path) in an Intent.
Remember the sending mail example ?

Example 1 : Serving Intents , Making your application a dialer

Intent Filters
In order define the intents your activity can handle, your intent filter should fulfills the criteria of the Intent object : Action , Data and Category (discussed next)

Your filter should be as specific as possible

Intent Filters Properties


Action: string defines the action to be performed like ACTION_SEND and ACTION_VIEW. Data: adds data specification scheme to the intent filter (description of the data). It is Uri or Type ( e.g. android:mimeType , text/plain or image/jpeg) Priority: about the priority of that activity over other activities that respond to the same type of intents. Category: Additional way to characterize the activity handling the intent

Intent Filter Examples

Intent Filter Examples

Broadcast Receiver

Broadcast Receiver
System (or application) sends broadcast messages Only interested Receivers will be fired and an intent with the broadcast message info is sent to them. Receiver must have an intent filter that matches the intent action, category and type to be notified. The broadcast receiver extends BroadcastReceiver and should be registered as a receiver in an Android Application via the AndroidManifest.xml ( or dynamically via registerReciver() method)

Receiving a phone ringing message

OnReceive Method

Menus

Menu Types
Option Menu Appears when you click the menu button in your device Context Menu

When long click for a view (Like right click)

Option Menu vs. Context menu

Option Menu

Menu in XML

Register the menu from Code


@Override public boolean onCreateOptionsMenu(Menu menu)

{
MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; }

Handling Item selection

Context Menu

Context Menu

Registering the context menu


registerForContextMenu (View view)

Menu Tutorial
http://developer.android.com/guide/topics/ui/menus.html

Das könnte Ihnen auch gefallen