Pro Android Learning Note (11): Learn about intent (middle)

Source: Internet
Author: User
Tags home screen

The composition of the intent

The intent can be action,data (expressed by a URI), extra data (Key/value map, key-value pairs), and the specified class name (becomes component name). A intent carries at least one of the above content.

Action. ACTION name, in the previous note has given two examples, one is the system comes with, such as Intent.action_dail, a developer is registered through Androidmanifest.xml, when creating Intent: Intent Intent =new Intent (String action_name);. The name prefix of the Action_name string is the class name.

Data. The format of a valid URI that is determined by a different action. Intent.setdata (Uri.parse ("xxxxxx"));. The evoked activity can be intent with activity.getintent () and then the intent GetData () to obtain the data.

Trigger party.
Intent i = new Intent (actionname);
String uristr = "wei://www.flowingflying.com";
I.setdata (Uri.parse (URISTR));
This.startactivity (i);


The trigger party. You need to register the data information in the Androidmanifest.xml in the Intent-filter, which is explained in detail later
Intent Intent = This.getintent ();
String data = intent.getdata ();

the common action is data passing . ACTION and arousal are not one-to-many, such as Intent.action_view based on data, evoking different applications. This one-to-many, general-purpose action, when registering in manifest, requires declaring the data (URI). For details, refer to: http://developer.android.com/guide/topics/manifest/data-element.html. The Action_view is distinguished by the schema. By class name, action name, and so on named explicit intent, can be a pair of more called implicit intent.

<activity......>
<intent-filter>
<action android:name= "Android.intent.action.VIEW"/>
<data android:scheme= "http"/>
<data android:scheme= "https"/>

</intent-filter>
</activity>

If our own intent to pass data, we also have to register data in manifest, otherwise intent will invoke the failure and report the activitynotfoundexception exception.

The more commonly used MIME type, such as registering <data android:mimetype= "vnd.android.cursor.dir/vnd.google.note"/> means to view a collection of notes (that is, the directory), The registered <data android:mimetype= "Vnd.android.cursor.item/vnd.google.note"/> means to view the specific item, that is, to view the specific note.

Extended Data (Extra) delivery . The way a URI restricts the amount of information passed. Extra format Key/value pairs, key names usually start with a package name, and value can be any underlying data type, or an object that implements Android.os.Parcelable.

Extra data is passed through the Intenttestdemo.java (evoking intentbasicviewactivity through intent i).
1, extra data in the form of bundles in the intent storage, if there are already bundles, the new key value pairs are added to it, if there is no bundle, then create one. Securing key uniqueness in the future
2, the key name usually starts with the package name, this example takes the constant way, actually is: cn.flowingflying.android.pro.extra.string.
3, the following is the most simple data type, or it can be an array, for example, Putextra (String name,int[]values);
Can be a serializable object, such as Putextra (String name,SerializableValue), and the Parcelable object, such as Putextra (String name,parcelablevalue);.
Bundles can be passed, such as Putextra (String name,BundleValue);
Intent can be passed, such as Putextra (String name,IntentAnotherintent);
You can support the array list:Putintegerarraylistextra(String name, ArrayList ArrayList);Putparcelablearraylistextra(String name, ArrayList ArrayList);Putstringarraylistextra(String name, ArrayList ArrayList)
Note: Instead of reference (pointers) in intent, instead of copy, we modify the object without affecting the data that has been copied into the bundle in the intent, which requires great care.
I.PutExtra(intentbasicviewactivity.extra_flowingflying_string, "Hello, intent!");

Get the extra data in Intentbasicviewactivity.java with the following code.
Intent Intent = This.getintent ();
Bundle B = Intent.Getextras();
String s = b.getstring (extra_flowingflying_string));

The Android system has a defined key value, specifically see Http://developer.android.com/reference/android/content/Intent.html#EXTRA_ALARM_COUNT

Use component to evoke activity

In the previous example, we use action name to invoke activity. For explicit intent, which explicitly specifies which activity to invoke, it can be called by giving the package name and class name of the component. Here we try to make calls in four similar ways, which are essentially no different in four ways.

private void BasicTest1 () {
Intent Intent = new Intent ();
Mode 1:setcomponent (componentname name);
intent.setcomponent (New ComponentName ("Cn.flowingflying.android.pro",
"Cn.flowingflying.android.pro.IntentBasicViewActivity"));
StartActivity (Intent);
}

To write the full class Name, cannot write intentbasicviewactivity or. Intentbasicviewactivity, otherwise the report could not find the activity error
private void BasicTest2 () {
Intent Intent = new Intent ();
Mode 2:setclassname (String packagename, string classnameinthatpackage);
intent.setclassname ("Cn.flowingflying.android.pro",
"Cn.flowingflying.android.pro.IntentBasicViewActivity");

StartActivity (Intent);
}
To write the full class Name, cannot write intentbasicviewactivity or. Intentbasicviewactivity, otherwise the report could not find the activity error
private void BasicTest3 () {
Intent Intent = new Intent ();
Way 3:setclassname (context context, String classnameinthatcontext);
Intent.setclassname (This, "cn.flowingflying.android.pro.IntentBasicViewActivity");
StartActivity (Intent);
}

private void BasicTest4 () {
Intent Intent = new Intent ();
Mode 4:setclass (context context, Class classobjectinthatcontext);
Intent.setclass (This,intentbasicviewactivity.class);
StartActivity (Intent);
}

Category attribute for intent

In Androidmanifest.xml, we can set the category of intent, for example:

<activity ......>
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

When the app launches Yes, it will look for activity labeled category_launcher to load. Android defines multiple CATEGORY, which can be viewed in Http://developer.android.com/reference/android/content/Intent.html#CATEGORY_ALTERNATIVE. For example, Category_home can be used as the home screen for the app, and Category_gadget is suitable for embedding into an activity.

Here are two examples. Example 1 indicates the action name, because there are multiple matches, the system will be listed for the user to choose, as follows.

Example 2 adds Intent.addcategory (Intent.category_launcher) on the basis of example 1, and by adding a match to the CATEGORY name, you can see that the system has been further filtered. :

We can get matching activity information by Packagemanager in code, without invoking intent. As follows

Intent Intent = new Intent (intent.action_main,null);
Intent.addcategory (Intent.category_launcher);
Packagemanager pm = Getpackagemanager ();
list<resolveinfo> list = pm.queryintentactivities (intent, 0);


For (ResolveInfo ri:list) {
String PackageName = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
... ....
With PackageName and className, analysis can be carried out and then through Intent i= new Intent (Packaname,classname); StartActivity (i); evoke the acitivity we need.
}

However, the number of matching activity obtained through packagemanager more than the number of activities that the system provides to the user in the example, why? In particular, this application is not shown. By category filtering belongs to the implicit intent call method, not the specified package name and class name explicit intent precise invocation method, for implicit intent calls need to be declared, as follows:

<activity android:name= "..." android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
<category android:name= "Android.intent.category.DEFAULT"/>
</intent-filter>
</activity>

There can be more than one category description in the activity's intent-filter, where category_default means you can use the implicit intent call, and when we add this declaration, This application appears in the list of matching activity. Similarly, for a call through action name, do not specify the package name and the class name, also belongs to implicit intent, the same need to make Category_default declaration, otherwise there will be activitynotfoundexception error. If the activity is not set to Category_default in intent fliter, we can use Packagemanager to get the matching activities information, and then get the exact package name and class name after the analysis. The activity is aroused by the explicit way.

In addition, Android said that if you do not need default from launcher screen, that is, acitivty only need main and launcher, of course, we can also set the default. Android appears to be a bit cumbersome on default, simply saying that if we don't want the app to be called by other apps via implicit, we don't set the default.

There is an interesting category in categories: <category android:name= "Android.intent.category.HOME"/> We have added this category to mainactivity and another activity.

<application. >
<activity. >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.HOME"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
<category android:name= "Android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name= ". Intentbasicviewactivity "android:label=" @string/intent_basic_test ">
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.HOME"/>
<category android:name= "Android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
......
</application>

The left image in the code through the Packagemanager to see the information matching category_home, in the figure through startactivity (intent) to evoke matching category_home activities, the system gives the user the choice, If you press the home key in the app, you have the same effect; the right picture exits the app, and when you press the home key, the system gives the user the option to enter the home UI.

RELATED Links: My Android development related articles

Pro Android Learning Note (11): Learn about intent (middle)

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.