Pro Android Study Notes (11): Learn about intent (medium)

Source: Internet
Author: User

Intent Composition

Intent can contain action, data (expressed by URI), extra data (key/value map, key-value Pair), and the specified class name (become component name ). An intent must carry at least one of the preceding content.

Action. The Action name. Two examples have been provided in the previous note. One is built-in, such as Intent. ACTION_DAIL. One is that developers use AndroidManifest. when you create an intent, the following information is displayed: Intent intent = new Intent (String action_name );. The prefix of the action_name string name is the class name.

Data. Different actions determine the valid URI format. Intent. setData (Uri. parse ("xxxxxx "));. The aroused activity can obtain the intent with activity. getIntent (), and then get the data through the getData () of the intent.

// Trigger.
Intent I = new Intent (actionName );
String uriStr ="Wei: // www.flowingflying.com";
I. setdata (URI. parse (uristr ));
This. startActivity (I );

// Triggered. You need to register data information in intent-filter in AndroidManifest. xml, which is described in detail later
Intent intent = this. getIntent ();
String data =Intent. getdata ();

Common actions are data transmission.. Action and wakeup are not one-to-one. For example, Intent. ACTION_VIEW invokes different applications based on data. This one-to-multiple, that is, a common action, must declare the data (URI) requirements when registering in manifest. For more information, see http://developer.android.com/guide/topics/manifest/data-element.html. ACTION_VIEW is differentiated by schema. The class name, action name, and other methods are used to specify an explicit
Intent, which can be one-to-multiple 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 intent needs to pass data, we must also register data in manifest; otherwise, intent will call up failure and report the exception of activitynotfoundexception.

MIME types are commonly used, such as registering <data Android: mimetype = "Vnd. android. cursor. DIR/vnd. google. note "/> indicates the set of notes (that is, the directory) to be viewed, and registered <data Android: mimetype =" Vnd. android. cursor. item/vnd. google. note "/> indicates to view the specific item, that is, to view the specific note.

Extra data Transmission. The URI method limits the amount of information transmitted. Extra format key/value pairs. The key name usually starts with the package name, and value can be any basic data type or an object that implements Android. OS. parcelable.

In intenttestdemo. Java (call intentbasicviewactivity through intent I), the extra data is transmitted below.
1. Extra data is stored in intent as bundle. If a bundle already exists, add the new key-value pair to it. If no bundle extra data is available, create one. Ensure key uniqueness in the future
2. The key name usually starts with the package name. In this example, it is in constant mode and is actually CN. flowingflying. Android. Pro. Extra. String.
3. The following is the simplest data type and can also be an array, such as putextra (string name,Int []Values );
It can be a serializable object, such as putextra (string name,SerializableValue); and parcelable object, such as putextra (string name,ParcelableValue );.
The bundle can be passed, such as putextra (string name,BundleValue );,
Intent can be passed, such as putextra (string name,IntentAnotherintent );
Array list is supported, as shown in figurePutIntegerArrayListExtra(String name, arraylist );PutParcelableArrayListExtra(String name, arraylist); andPutStringArrayListExtra(String name, arraylist)
[Note] What is passed in intent is not a reference (pointer), but a copy. Modifying the object does not affect the data that has been copied to the bundle in intent, so pay attention to this.
I.PutExtra(IntentBasicViewActivity. EXTRA_FLOWINGFLYING_STRING, "Hello, intent! ");

In intentbasicviewactivity. Java, use the following code to obtain extra data.
Intent intent = This. getintent ();
Bundle B = intent.GetExtras();
String S = B. getstring (EXTRA_FLOWINGFLYING_STRING));

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

Use Component to evoke Activity

In the previous example, we call activity by action name. For explicit intent, that is, specifying the activity to be called, you can call it by giving the package name and Class Name of the component. In the following experiment, we use four similar methods for calling. These four methods are essentially the same.

Private void basicTest1 (){
Intent intent = new Intent ();
// Method 1: setComponent (ComponentName name );
Intent. setcomponent (New componentname ("cn. flowingflying. Android. Pro ",
"Cn. flowingflying. Android. Pro. intentbasicviewactivity "));
StartActivity (intent );
}

// To write the complete class Name, you cannot write IntentBasicViewActivity or. IntentBasicViewActivity. Otherwise, the error of Activity cannot be found in the report.
Private void basicTest2 (){
Intent intent = new Intent ();
// Method 2: setClassName (String packageName, String classNameInThatPackage );
Intent. setclassname ("cn. flowingflying. Android. Pro ",
"Cn. flowingflying. Android. Pro. intentbasicviewactivity ");

StartActivity (intent );
}
// To write the complete class Name, you cannot write IntentBasicViewActivity or. IntentBasicViewActivity. Otherwise, the error of Activity cannot be found in the report.
Private void basicTest3 (){
Intent intent = new Intent ();
// Method 3: setClassName (Context context, String classNameInThatContext );
Intent. setclassname (this, "cn. flowingflying. Android. Pro. intentbasicviewactivity ");
StartActivity (intent );
}

Private void basicTest4 (){
Intent intent = new Intent ();
// Method 4: setClass (Context context, Class classObjectInThatContext );
Intent. setclass (this, intentbasicviewactivity. Class );
StartActivity (intent );
}

Category attribute of 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 an application is started, the activity to be searched is marked as category_launcher to load. Android defines multiple category items, which can be viewed at http://developer.android.com/reference/android/content/intent.html?category_alternative. For example, category_home can be used as the home of the application.
Screen, while category_gadget is suitable for embedding in an activity.

The following are two examples. Example 1 specifies the action name. Because there are multiple matches, the system will list them for your selection, as shown below.

In Example 2, intent. addCategory (Intent. CATEGORY_LAUNCHER) is added based on Example 1, and the matching of Category names is added. We can see that the system performs further filtering. :

We can use PackageManager in the code to obtain the matching activity information without arousing 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, you can perform analysis and call out the required acitivity through Intent I = new Intent (packaname, className); startActivity (I.
}

However, the number of matched activities obtained through PackageManager is greater than the number of activities provided by the system to the user in the example. Why? In particular, this application is not displayed. Use category to filter call methods that belong to implicit intent, not the explicit call method of the specified software package name and class name. For implicit
The intent call must 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 multiple category descriptions in the intent-filter of the activity. CATEGORY_DEFAULT indicates that implicit intent can be called. When this declaration is added, this application will appear in the matched activity list. Similarly, for calls by action name, if the package name and class name are not specified, they also belong to the implicit intent, and the CATEGORY_DEFAULT Declaration is also required. Otherwise, the ActivityNotFoundException error may occur. If the activity is not in the intent
Set "CATEGORY_DEFAULT" in fliter. We can use PackageManager to obtain information about the matched activities. After analysis, we can get the exact package name and class name, and call the activity through "explicit.

In addition, Android says that if you do not need DEFAULT when calling the launcher screen, that is, at this time, acitivty only needs MAIN and LAUNCHER. Of course, we can also set it to DEFAULT. Android seems complicated on DEFAULT. Simply put, if we do not want the App to be called by other apps through implicit, we should not set DEFAULT.

There is an interesting category in category: <category android: name = "android. intent. category. HOME"/>. we add this category in 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>

In the code, the left chart uses PackageManager to view information matching CATEGORY_HOME. When StartActivity (intent) is used to evoke Activities matching CATEGORY_HOME, the system selects the user. If you press the Home Key in the application, the result is the same. The right figure shows how to exit the application. Press the Home Key and ask the system to give the user a choice when entering the Home UI.

Related links:
My Android development articles

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.