Pro Android Learning Note (12): Learn intent (bottom)

Source: Internet
Author: User

Parse intent, find matching activity

If given component name (package name, class name) is explicit intent, otherwise implicit intent. For explicit intent, the key is component name, and other attributes declared in <intent-fliter> are ignored. For implicit intent, the match is based on action,category and data. However, a intent fliter can declare multiple actions, multiple categories, multiple data properties, and therefore can satisfy different intent.

For implicit intent set action, the action must exist in a <action nandroid:name= "..." > below <intent-fliter>. But unlike the one written in Pro Android 4.0, after testing, we found that the action statement must exist under Intent-fliter.
1) for intent that have the action set, the action cannot be matched under intent-fliter;
2) for intent that do not have an action set, it does not match the case where the action is not defined under Intent-fliter;
3) for intent that do not have an action set, you can match any action defined under Intent-fliter, but intent should provide sufficient filtering criteria, such as the URI of data (the test display does not match using category filtering).
At least in the Android 4.2 version of the test. Therefore, we should declare in Inter-fliter that action Name,intent should provide the action name, which, if special cases are not available, requires a data property to be filtered. But this particular situation should not occur, in other words, we have to do action matching.

If implicit intent sets the data schema, it must be able to match intent fliter a data schema, if data intent is set under Fliter schema,implicit The intent must provide a valid data URI and cannot be empty. For URIs, in addition to schema, you can also have requirements for authority (that is, domain), which is defined in manifest as follows.

<data android:scheme= "Wei" android:host= "flowingflying.com"/>

The matching URI is: wei://flowingflying.com/.... can also be further restricted by Android:path,android:pathprefix,android:pathpattern. Developer.android.com/guide/topics/manifest/data-element.htm.

Similarly, for data mimetype, there must be a match between the two parties, and there is no such thing as a match. Set by SetType (), such as Intent.settype ("text/html"), in addition, for subtype, you can use * as a wildcard, such as Intent.settype ("text/*");

Action_pick

Activity a evokes activity B through intent, and in some cases we want B to return information at the end to a to determine the next process, which is also a way of using B as the UI. A common way is that B is the UI of list lists, here is a small example, we do not specifically implement B's list UI, just simulate select item after the set return value and return information, the return information is carried by intent, this example will automatically turn off B.

Activity B Step 1: Add pick action in intent Fliter of Andriodmanifest.xml b

<activity. >
<intent-filter>
... ...
<data android:scheme= "Wei"/>
<action android:name= "Android.intent.action.PICK"/>
<category android:name= "Android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

Activity B Step 2: Set the return value and return information in B, this example returns information that simulates a Notepad URI and automatically turns off B

......
private void Testpick (int itemIndex) {
Since intent Fliter can have multiple actions, it needs to be filtered first, and only the action of pick will return the value.
IfIntent.ACTION_PICK.equals (This.getintent (). Getaction ())){
Set the return value RESULT_OK, and carry the information through intent at the same time, this example passes the URI through data
Setresult (ACTIVITY.RESULT_OK/* return value */,
New Intent ().
SetData (Getnoteuri (ItemIndex))/ * intent*/that carry information);
Automatically closes activity b, and the return value is passed to a when B ends. Of course, you can also manually end B, such as pressing the system return key.
finish ();
}
}

Private Uri Getnoteuri (int itemIndex) {
Return Uri.parse ("content://com.google.provider.notepad/notes/" + itemIndex);
}

Activity A:

private static final int pick_request_code = 1000;

private void Invokepick (activity activity) {
Set the intent,action of evoke B to pick, in this case the SetData is just a match for implicit
Intent Intent = new Intent (Intent.action_pick);
Intent.setdata (Uri.parse ("Wei://flowingflying.com/notes"));
Do not use startactivity, because startactivity is similar to opening a modal dialog and cannot be callback, so Startactivityforresult () is used
Activity.startactivityforresult (Intent, Pick_request_code/*request code*/);Since multiple different activity can be pick, all callback trigger the Onactivityresult () method, which is used to differentiate using request code.
}

Here is the Onactivityresult () method that overrides the activity, which is triggered when B ends, where the first parameter requestcode is carried in Startactivityforresult (), Can be judged to be returned from that activity; The second parameter is the return value of B, with the system value RESULT_OK (-1), result_canceled (1), or user-defined value, the custom value must start with Result_first_user (1) The third parameter is the return information that B carries with intent
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent outputintent){
TODO auto-generated Method Stub
Super.onactivityresult (Requestcode, ResultCode, outputintent);
This example only handles the return of activity B, whose REQUEST CODE is Pick_request_code) {
if (requestcode! = Pick_request_code) {
LOG.D ("WEI", "Some one else called this. not us");
Return
}
If the return value is not OK, that is result_canceled, do not process
if (resultcode! = ACTIVITY.RESULT_OK) {
LOG.D ("WEI", "Result is not OK");
Return
}
Read the information intent carries
Uri uri = Outputintent.getdata ();
LOG.D ("WEI", "uri is" + uri.tostring ());

Processing according to carrying information: This example B returns a Notepad content Uri with which to open the Notepad. Notepad is not available on the simulator, we can import it through sample, see the last attached Benbow
StartActivity (New Intent (Intent.action_edit,uri));
}

Action_get_content

Action_get_content and Action_pick are very similar, Action_pick is to give the URI, the system gives one or more matching choices to the user, after execution, the aroused activity return value to the caller. The action_cet_content is not based on the URI, but on the mime Type.

This example uses Notepad, please refer to Benbow last attached: Install Notepad for simulator. Let's look at the manifest.xml file for sample Notepad first. This document shows the coexistence of multiple intent-fliter, starting with the application, pick evoking and get_content evoking three cases.

<activity android:name= "noteslist" android:label= "@string/title_notes_list" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name= "Android.intent.action.VIEW"/>
<action android:name= "Android.intent.action.EDIT"/>
<action android:name= "Android.intent.action.PICK"/>
<category android:name= "Android.intent.category.DEFAULT"/>
<data android:mimetype= "Vnd.android.cursor.dir/vnd.google.note"/>
</intent-filter>
<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>

Code writing is similar to pick:

private static final int request_code_get_content = 101;

private void invokegetcontent (activity activity) {
Intent Intent = new Intent (intent.action_get_content);
Intent.settype ("Vnd.android.cursor.item/vnd.google.note");Get_content for MIME TYPE
Activity.startactivityforresult (Intent, request_code_get_content);
}

@Override//Receive acivity return information
protected void Onactivityresult (int requestcode, int resultcode, Intent outputintent){
if (resultcode! = ACTIVITY.RESULT_OK) {
LOG.D ("Wei", "Result is not OK");
Return
}

Switch (requestcode) {
Case Request_code_pick:
...//handling of Action_pick, refer to the previous example
Break
Case Request_code_get_content:
Uri uricontent = Outputintent.getdata ();
LOG.D ("Wei", "uri is" + uricontent.tostring ());
StartActivity (New Intent (intent.action_edit,uricontent));
Break
Default
Showinfo ("Some one else called this. Not us ");
Break
}
}

Pending Intent

Intent can be used as a parameter in the pendingintent. Pengdingintent, as the name implies, is not called immediately and can be aroused after an event has been triggered. For example, to use in the notification, get the user to click on the notification only to adjust, see Android Learning Note (May Fourth): Notify notification (above); For example, warning, such as location matching Si Cho, see Android Learning note (five or six): location. The instant-call process (APP) no longer exists, and pendingintent is still well preserved, which makes it easy for the process to end or other process calls.

Let's do a little experiment and see how pendingintent is built:

public static Pendingintent getactivity (context context, int requestcode, Intent Intent, int flags)
public static Pendingintent Getactivities (context context, int requestcode, intent[] intents, int flags)

Intent Intent = new Intent (this,intentbasicviewactivity.class); Ordinary intent.
pendingintent pi = pendingintent.getactivity (Getapplicationcontext (), 0,intent,0);
Showinfo ("intent is" + intent); Showinfo () is also displayed in the Logcat and UI windows
Showinfo ("Pending intent is" + pi);
Pendingintent pi1 = pendingintent.getactivity (Getapplicationcontext (),0, intent,0);
Showinfo ("Pending intent is" + pi1);
Pendingintent pi2 = pendingintent.getactivity (Getapplicationcontext (),1, intent,0);
Showinfo ("Pending intent is" + pi2);

Why use getactivity in such a peculiar way. Normal intent can evoke activity, turn on services and evoke intent through startactivity (intent), StartService (Sendbroadcast) and intent (broadcast) Receiver As a result of the intent parameters in the pendingintent to actually evoke the corresponding activity, service or broadcast receiver, it is necessary to tell the system is activity, service or broadcast. The corresponding will also be:

Getbroadcast (context context, int requestcode, Intent Intent, int flags)
GetService (context context, int requestcode, Intent Intent, int flags)

As for why using getxxxx this way, according to "Pro Android 4.0" is explained: Android will store pendingintent, and can be reused (depending on the flag parameter settings), if you need to use the same intent, Need to get the same pendingintent, all with Get. If you want to differentiate, you can set different Requestcode.

For the flag parameter, there are:

"1" flag_cancel_current: If an identical pendingintent object already exists in the current system, the existing pendingintent is canceled and a Pendingintent object is regenerated.
"2" flag_no_create: If the same Pendingintent object does not exist in the current system, the system will not create the Pendingintent object and return null directly.
"3" Flag_one_shot: The pendingintent only works once. After the Pendingintent object is triggered by the Send () method, Pendingintent will automatically call cancel () to destroy, and if you call the Send () method again, the system will return a sendintentexception.
"4" Flag_update_current: If there is a pendingintent equivalent pendinginent that you describe in the system, then the Pendingintent object will be used by the system. However, the new intent is used to update the intent object data in the previous pendingintent, such as extras in the update intent.
"description" This is the interpretation of several flags, refer to from http://blog.csdn.net/hudashi/article/details/7060837, more please read http://developer.android.com/ Reference/android/app/pendingintent.html#flag_cancel_current

If we get a pendingintent to evoke the intent, we can use

Pi. Send (Result_code); Pendinginten Multiple Send methods

Attached: Installing Notepad for simulator

Download the sample code from the Android SDK Manager and then add it to the emulator based on the action.

In order to have a better user experience in pick and Get_content calls, the NotePad activity can be turned off automatically when the user selects item. We add the following line of code to the Noteslist.java:

protected void Onlistitemclick (ListView l, View v, int position, long ID) {
......
Handles Requests for note data
if (Intent.ACTION_PICK.equals (ACTION) | | Intent.ACTION_GET_CONTENT.equals (ACTION)) {

Sets the result to return to the component that called this Activity.
Result contains the new URI
Setresult (RESULT_OK, New Intent (). SetData (URI));
//wei Add the Following:close notelist automatically
finish (); Wei Add
......
}

RELATED Links: My Android development related articles

Pro Android Learning Note (12): Learn intent (bottom)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.