2011年5月19日木曜日

AndroidStudyMemo=Acting as a Content Type Handler

Your application can act as a content type filter?that is, handle common intent actions

such as VIEW, EDIT, or SEND for specific MIME types.

 

A photo application might act as a content type handler for VIEW actions for any graphic

formats, such as JPG,PNG, or RAW image file MIME types. Similarly, a social networking

application might want to handle intent SEND actions when the underlying data has a

MIME type associated with typical social content (for example, text, graphic, or video).

This means that any time the user tries to send data (with the MIME types that the social

networking application was interested in) from an Android application using an Intent

with action SEND, the social networking application is listed as a choice for completing the

SEND action request. If the user chooses to send the content using the social networking

application, that application has to launch an Activity to handle the request (for example,

an Activity that uploads the content to the social networking website to share).

 

Finally, content type handlers make it easier to extend the application to act as a content

provider, provide search capabilities, or include live folder features. Define data

records using custom MIME types, so that no matter how an Intent fires (inside or outside

the application), the action is handled by the application in a graceful fashion.

To enable your application to act as a content type handler, you need to make several

changes to your application:

Determine which Intent actions and MIME types your application needs to be able

  to handle.

You need to implement an Activity that can process the Intent action or actions

  that you want to handle.

You need to register that Activity in your application's Android Manifest file using

  the <activity> tag as you normally would.You then need to configure an

  <intent-filter> tag for that Activity within your application's Android Manifest

  file, providing the appropriate intent action and MIME types your application can

  process.

 

Determining Intent Actions and MIME Types

Let's look at a simple example.we make various modifications to a simple field notes

application that uses a content provider to expose African game animal field notes;

each note has a title and text body.Throughout these examples, the application acts as a

content type handler for VIEW requests for data with a custom MIME type:

              vnd.android.cursor.item/vnd.androidbook.live.fieldnotes

 

MIME types come in two forms. Most developers are familiar with MIME types, such as

text/plain or image/jpeg (as defined in RFC2045 & RFC2046), which are standards

used globally. The Internet Assigned Numbers Authority (IANA) manages

these global MIME types.

 

Implementing the Activity to Process the Intents

Next the application needs an Activity class to handle the Intents it receives. For the

sample,we simply need to load a page capable of viewing a field note. Here is a sample

implementation of an Activity that can parse the Intent data and show a screen to displays

the field note for a specific animal:

              public class SimpleViewDetailsActivity extends Activity {

                            @Override

                            protected void onCreate(Bundle savedInstanceState) {

                                          super.onCreate(savedInstanceState);

                                          setContentView(R.layout.details);

 

                                          try {

                                                        Intent launchIntent = getIntent();

 

                                                        Uri launchData = launchIntent.getData();

                                                        String id = launchData.getLastPathSegment();

                                                        Uri dataDetails = Uri.withAppendedPath

                                                                      (SimpleFieldnotesContentProvider.CONTENT_URI, id);

 

                                                        Cursor cursor =

                                                                      managedQuery(dataDetails, null, null, null, null);

 

                                                        cursor.moveToFirst();

                                                        String fieldnoteTitle = cursor.getString(cursor

                                                                                    .getColumnIndex(SimpleFieldnotesContentProvider

                                                                                                  .FIELDNOTES_TITLE));

                                                       

                                                        String fieldnoteBody = cursor.getString(cursor

                                                                                    .getColumnIndex(SimpleFieldnotesContentProvider

                                                                                                  .FIELDNOTES_BODY));

                                                       

                                                        TextView fieldnoteView = (TextView)

                                                                      findViewById(R.id.text_title);

                                                        fieldnoteView.setText(fieldnoteTitle);

                                                       

                                                        TextView bodyView = (TextView) findViewById(R.id.text_body);

                                                        bodyView.setLinksClickable(true);

                                                        bodyView.setAutoLinkMask(Linkify.ALL);

                                                        bodyView.setText(fieldnoteBody);

                                          } catch (Exception e) {

                                                        Toast.makeText(this, "Failed.", Toast.LENGTH_LONG).show();

                                          }

                            }

              }

             

The SimpleViewDetailsActivity class retrieves the Intent that was used to launch the

Activity using the getIntent() method. It then inspects the details of that intent,

extracting the specific field note identifier using the getLastPathSegment() method.The

rest of the code simply involves querying the underlying content provider for the appropriate

field note record and displaying it using a layout.

 

Registering the Intent Filter

Finally, the Activity class must be registered in the application's Android manifest file and

the intent filter must be configured so that the application only accepts Intents for specific

actions and specific MIME types. For example, the SimpleViewDetailsActivity would

be registered as follows:

              <activity

                            android:name="SimpleViewDetailsActivity">

                            <intent-filter>

                                          <action

                                                        android:name="android.intent.action.VIEW" />

                                          <category

                                                        android:name="android.intent.category.DEFAULT" />

                                          <data android:mimeType =

                                                        "vnd.android.cursor.item/vnd.androidbook.live.fieldnotes" />

                            </intent-filter>

              </activity>

 

The <activity> tag remains the same as any other.The <intent-filter> tag is what's

interesting here. First, the action that the application wants to handle is defined using an

<action> tag that specifies the action the application can handle is the VIEW action.The

<category> tag is set to DEFAULT, which is most appropriate, and finally the <data> tag

is used to filter VIEW Intents further to only those of the custom MIME type associated

with field notes.

 

 

 

0 件のコメント:

コメントを投稿