Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Android Intents and intent filters By Shinping R. Wang CSIE Dept. of Da‐yeh University Reference The following discussion is excerpted from Intents and intent filters http://developer.android.com/guide/topics/intents/intentsfilters.html 1. Intent Three of the core components of an application — activities, services, and broadcast receivers — are activated through messages, called intents. Intent Runtime binding/late binding An Intent provides a facility for performing late runtime binding between the code in different applications. Intent Intent serves as glue between activities. is basically a passive data structure holding an abstract description of an action to be performed. Intent There are separate mechanisms for delivering intents to each type of component: Targeting component types Corresponding methods for delivering an intent object Activity Context.startActivity() Activity.startActivityForResult() Activity.setResult() Service Context.startService() Context.bindService() Broadcast receiver Context.sendBroadcast() Context.sendOrderedBroadcast() For example, To invoke a started service, you call Context.startService(intent) With associated intent Activity.setResult() Context.sendStickyBroadcast() Intent Objects In general, an intent object contains following information: Types Description Component name the name of the component that should handle the intent Action a string naming the action to be performed Data the URI and the MIME type of the data to be acted on. Category a string about the kind of component that should handle the intent. Extras Key-value pairs for additional information to the target component. Flags flags of various control purposes. Intent Objects Component name, The name of the component that should handle the intent. fully qualified class name of the target component for example "com.example.project.app.FreneticActivity“ Fully qualify name : package name + class name com.example.project.app.FreneticActivity Package name: Class name: com.example.project.app FreneticActivity The component name is set by setComponent(), setClass(), or setClassName() and read by getComponent(). Intent Objects Component name, The package name or the whole component name is optional. If it is not used, the system will resolve the target component using other information. If it is used however, the specified component will supersede others information. Intent Objects Action, A string naming the action to be performed or, If broadcast intents, the action that took place and is being reported. Intent Objects Action, The following list is not inclusive, For more generic predefined actions, see Intent. Constant Target component Action ACTION_CALL activity Initiate a phone call. ACTION_EDIT activity Display data for the user to edit. ACTION_MAIN activity Start up as the initial activity of a task, with no data input and no returned output. ACTION_SYNC activity Synchronize data on a server with data on the mobile device. ACTION_BATTERY_LOW broadcast receiver A warning that the battery is low. ACTION_HEADSET_PLUG broadcast receiver A headset has been plugged into the device, or unplugged from it. ACTION_SCREEN_ON broadcast receiver The screen has been turned on. ACTION_TIMEZONE_CHANGED broadcast receiver The setting for the time zone has changed. Intent Objects Action, User defined action for activating the components in your application is possible, User defined action should prefix with the application package name, for example: "com.example.project.SHOW_COLOR". Intent Objects Action, The action in an Intent object is set by the setAction() method and read by getAction(). Intent Objects Data Each data element can specify a URI and a data type (MIME media type). Different actions are paired with different kinds of data specifications. Intent Objects Data Action and data are related For example, Action field inference Data field URI ACTION_EDIT URI of the document to be displayed for editing ACTION_CALL tel: ACTION_VIEW http: Intent Objects Data Related Methods Method name Description setData() Specifies the data URI setType() Specifies the data MIME type setDataAndType() Specifies both the data URI and MIME type getData() Retrieves the data URI getType() Retrieves the data MIME type Intent Objects Category A string containing additional information about the kind of component that should handle the intent. Constant Meaning CATEGORY_BROWSABLE The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message. CATEGORY_GADGET The activity can be embedded inside of another activity that hosts gadgets. CATEGORY_HOME The activity displays the home screen, the first screen the user sees when the device is turned on or when the Home button is pressed. CATEGORY_LAUNCHER The activity can be the initial activity of a task and is listed in the top-level application launcher. CATEGORY_PREFERENCE The target activity is a preference panel. See the Intent class description for the full list of categories. Intent Objects Category Related methods Constant Meaning addCategory() places a category in an Intent object removeCategory() deletes a category previously added Intent Objects Extras Key-value pairs for additional information that should be delivered to the component handling the intent. Intent Objects Extras For example, ACTION_TIMEZONE_CHANGED intent has a "time-zone" extra that identifies the new time zone, and ACTION_HEADSET_PLUG has a "state" extra indicating whether the headset is now plugged in or unplugged, as well as a "name" extra for the type of headset. Intent Objects Extras Related methods There exist many methods for different extras value types, see putExtras() and getExtras() for detail. Intent Objects Flags Flags of various sorts. Many instruct the Android system how to launch and treat an activity. Intent Objects Invoking applications that come with the Android platform http://developer.android.com/guide/appendix/g-appintents.html Browser Dialer Google Maps Google Street view Intent filters Intents can be divided into two groups: Explicit intents designate the target component by its name. typically used for application-internal messages. Implicit intents typically used to activate components in other applications. Intent filters For implicit intents Android system must resolve the best component (or components) from the information carried by the intent to handle request. Android system does this by comparing the contents of the Intent object to intent filters of all candidate components. Intent filters Only three entries of an Intent object are consulted when the object is tested against an intent filter: Action data (both URI and data type) Category To start the requested component, all three entries of the intent have to match the corresponding entries of the intent filter of the requested component. Intent filters Intent filters Component (activities, services, and broadcast receivers) has set of intent filters that describe its capabilities.† Implicitly intents are either filtering in or out using these filters. †Component without intent filter takes only explicit intent. Intent filters Intent filters Activities, services, content provider define theirs’ intent filters in AndroidManifest.xml † Broadcast receivers do it differently. It uses Context.registerReceiver() to create intent filter dynamically. †The system has to know the capabilities of these components during installation. Intent filters For example, The registerBroadCastReceiver() of ClockBackService.java /ApiDemos/src/com/example/android/apis/accessibility/Clock BackActivity.java private void registerBroadCastReceiver() { // Create a filter with the broadcast intents we are interested in. IntentFilter filter = new IntentFilter(); filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); // Register for broadcasts of interest. registerReceiver(mBroadcastReceiver, filter, null, null); } Intent filters For example, The NoteEditor Activity of Note Pad application sample. There are two intent filters, <activity android:name="NoteEditor" android:theme="@android:style/Theme.Light" android:label="@string/title_note" > <intent-filter android:label="@string/resolve_edit"> <action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.EDIT" /> <action android:name="com.android.notepad.action.EDIT_NOTE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.INSERT" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> </intent-filter> </activity> starting up with a specific note that the user can view or edit starting with a new, blank note that the user can fill in and save Intent filters http://developer.android.com/reference/android/content /Intent.html#ACTION_VIEW public static final String ACTION_VIEW Since: API Level 1 Activity Action: Display the data to the user. This is the most common action performed on data -- it is the generic action you can use on a piece of data to get the most reasonable thing to occur. For example, when used on a contacts entry it will view the entry; when used on a mailto: URI it will bring up a compose window filled with the information supplied by the URI; when used with a tel: URI it will invoke the dialer. Input: getData() is URI from which to retrieve data. Output: nothing. Constant Value: “android.intent.action.VIEW“ <activity android:name="NoteEditor" android:theme="@android:style/Theme.Light" android:label="@string/title_note" > <intent-filter android:label="@string/resolve_edit"> <action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.EDIT" /> <action android:name="com.android.notepad.action.EDIT_NOTE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </intent-filter> Intent filters To start the requested component, all three entries (Action, Data, and Category) have to be unlocked all together. Intent Requesting Component Intent Filter Action Requested Component Data Category This does not mean there is no way that the requested component can be reached. Do remember, there are other set of intent filters that the requesting component might get through! Action test For example, and intent-filter element with action subelements <intent-filter . . . > <action android:name="com.example.project.SHOW_CURRENT" /> <action android:name="com.example.project.SHOW_RECENT" /> <action android:name="com.example.project.SHOW_PENDING" /> ... </intent-filter> a filter must contain at least one <action> element, or all implicit intents will be blocked. to pass the filtering, at least one of the <action> element has to be matched. Action test What if action is missing either in the intent filter or an intent? Intriguing…. If the filter fails to list any actions, no intents can get through the filter. If an Intent object doesn't specify an action, it will automatically passes the test — as long as the filter contains at least one action. Category test An <intent-filter> element with categories as subelements. For example: <intent-filter . . . > <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> ... </intent-filter> Noticed that, more then one category can be listed inside of the filter. Be awarded, only use full qualify name for the element value in the intent filter of the manifest file. However, constant name can be used in the intend object. For example, we use android.intent.category.BROWSABLE in the filter (at the manifest file) while CATEGORY_BROWSABLE is used in the corresponding intent object. Category test Every <category> in the intent has to be matched with one in the intent filter, but not vice versa. The intent filter may contain more <category> than an intent. This says, an Intent object with no categories should always pass this test, regardless of what's in the filter. Category test Activities that are willing to receive implicit intents must include "android.intent.category.DEFAULT" in their intent filters.† Each intent however, does not have to specify “android.intent.category.DEFAULT” category when using startActivity() . †Filters with "android.intent.action.MAIN" and "android.intent.category.LAUNCHER" settings are the exception. They can put "android.intent.category.DEFAULT" in the filter but they don’t need to. Data test <data> in the intent filter is contained in a subelement. <data> in the intent filter can appear multiple times, or not at all. For example: <intent-filter . . . > <data android:mimeType="video/mpeg" android:scheme="http" . . . /> <data android:mimeType="audio/mpeg" android:scheme="http" . . . /> ... </intent-filter> Each <data> element can specify a URI and a data type (MIME media type). Data test Each <data> element can further divide into a URI and a data type (MIME media type) entry. Both URI and data type entries are optional. Data test For URI entry, The URI contains following parts — scheme, host, port, and Data test A URI Example, for each part of the URI: scheme://host:port/path content://com.example.project:200/folder/subfolder/etc scheme host:port or authority path Data test For URI When the URI in an Intent object is compared to a URI specification in a filter, only attributes that listed in the filter matters, What the gatekeeper(filter) says count!! The visitor(intent object) has to follow. For example, if a filter specifies only a scheme, all URIs in the intent object with that scheme match the filter so on so forth (kind of redundant.) Data test For URI, a path specification in the filter can contain wildcards to require only a partial match of the path. content://com.example.project:200/folder/subfolder/etc* Data test For data type entry, specifies the MIME type of the data. can use a "*" wildcard for the subtype field. For example, "text/*" or "audio/*" — indicating any subtype matches(? What will a subtype of type text be ?). Data test Matching the data between intent and intent filter. In general, for any entry in the filter, there must be a corresponding matching entry in the intent. filter URI Data type URI Data type intent Data test Matching the data between intent and intent filter. For URI like mailto: and tel: which do not refer to actual data, only matching URI entry in intent and filter is sufficient. mailto: tel: filter mailto: tel: intent Data test Matching the data between intent and intent filter. For component that support content: and file:, its filter can neglect the URI and carry only the data type entry. The corresponding intent with URI like content: and file: and matching data type is sufficient. content: file: Data type filter Data type intent Note Pad Example <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.notepad"> <application android:icon="@drawable/app_notes" android:label="@string/app_name" > The first activity, NotesList consists of two sets of intent filter. <provider android:name="NotePadProvider" android:authorities="com.google.provider.NotePad" /> <activity android:name="NotesList" android:label="@string/title_notes_list"> <intent-filter> This activity is the main activity of the <action android:name="android.intent.action.MAIN" /> application. It is the launcher of the <category android:name="android.intent.category.LAUNCHER" /> application. </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> Declares things that the activity can <action android:name="android.intent.action.EDIT" /> do on a directory of notes: VIEW, EDIT, <action android:name="android.intent.action.PICK" /> PICK. <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> </intent-filter> Note Pad Example <intent-filter> <action android:name="android.intent.action.GET_CONTENT" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </intent-filter> </activity> <activity android:name="NoteEditor" android:theme="@android:style/Theme.Light" android:label="@string/title_note" > <intent-filter android:label="@string/resolve_edit"> <action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.EDIT" /> <action android:name="com.android.notepad.action.EDIT_NOTE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.INSERT" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" /> </intent-filter> </activity> Note Pad Example <activity android:name="TitleEditor" android:label="@string/title_edit_title" android:theme="@android:style/Theme.Dialog"> <intent-filter android:label="@string/resolve_title"> <action android:name="com.android.notepad.action.EDIT_TITLE" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.ALTERNATIVE" /> <category android:name="android.intent.category.SELECTED_ALTERNATIVE" /> <data android:mimeType="vnd.android.cursor.item/vnd.google.note" /> </intent-filter> </activity> </application> </manifest> Intent filters Data test As general rules, when matching intent object to the filter, What the filter says count. What the filter doesn’t say, doesn’t matter. About our final project, Due date: 18 June, 2012 Submit a written report + source in a .rar package 1. Title, student ID(s) 2. Introduction 3. Design description (spec., UML drawing, and et c) 4. Implementation (codes segment , run time screenshots) 5. Conclusion + APK and source. and mail to [email protected].