Today we'll talk about the principles and applications of intent in Android.
We've summarized several important components of Android, and I believe we have a clear understanding of these components, so let's take a look at a few common actions:
- Start a Activity:Context.startActivity (Intent Intent);
- Start a Service:Context.startService (Intent service);
- Bind a Service:Context.bindService (Intent service, serviceconnection conn, int flags);
- Send a Broadcast:Context.sendBroadcast (Intent Intent);
We found that in all of these operations, there is a intent involved, which looks like a very important component, so what is intent?
In a nutshell, intent is the data payload of data transfer between components of a system. When we need to do a call action, we can tell the Android system through intent to complete the process, intent is an action to invoke the notification.
Intent has several important attributes, and we'll explain each of these:
1.action, the action to be performed
For an activity with the following declaration:
<activity android:name= ". Targetactivity ">
<intent-filter>
<action android:name=" Com.scott.intent.action.TARGET "/>
<category android:name= "Android.intent.category.DEFAULT"/>
</intent-filter>
</ Activity>
Targetactivity the <action> in its <intent-filter>, the target action, and if we need to do a jump action, we need to specify the action of the target in intent, as follows:
public void gototargetactivity (view view) {
Intent Intent = new Intent ("Com.scott.intent.action.TARGET");
StartActivity (Intent);
When we specify the appropriate action for intent, and then call the StartActivity method, the system jumps to the corresponding activity according to the action.
In addition to the custom action, intent also contains a number of default action, casually enumerating several:
public static final String Action_main = "Android.intent.action.MAIN";
public static final String Action_view = "Android.intent.action.VIEW";
public static final String Action_web_search = "Android.intent.action.WEB_SEARCH";
Each action has its specific purpose, and they are also used below.
2.data and extras, the data to be manipulated by the action and additional information to be delivered to the target
Here's an example of interacting with browsers:
/**
* Open the specified page
* @param view/public
void Invokewebbrowser (view view) {
Intent Intent = new Intent (in Tent. Action_view);
Intent.setdata (Uri.parse ("http://www.google.com.hk"));
StartActivity (intent);
}
/**
* Keyword search
* @param view/public
void Invokewebsearch (view view) {
Intent Intent = new inte NT (intent.action_web_search);
Intent.putextra (Searchmanager.query, "Android"); Keywords
startactivity (intent);
The above two methods are to start the browser and open the specified Web page, keyword search, the corresponding ACTION is Intent.action_view and Intent.action_web_search, the former need to specify the corresponding Web page address, the latter need to specify keyword information, For keyword searches, browsers search by default search engines set by themselves.
We note that when you open a Web page, specify a data property for intent, which is actually specifying what you want to manipulate, which is the form of a URI, where we can convert a string of the specified prefix to a specific URI type, such as "http:" or "https:" for the network address type, " Tel: "Indicates the type of phone number," mailto: "indicates the type of mail address, and so on. For example, to call a given number, you can do this:
public void call (view view) {
Intent Intent = new Intent (intent.action_call);
Intent.setdata (Uri.parse ("tel:12345678"));
StartActivity (intent);
}
So how do we know if the target accepts this prefix? This requires a look at the matching rules for the <data/> elements in the target.
The following seed elements are included in the target <data/> tag, and they define the matching rules for the URL:
- Android:scheme matches the prefix in the URL, except "http", "https", "tel" ... Besides, we can define our own prefixes.
- Android:host matches the host name portion of the URL, such as "google.com", or any host name if defined as "*"
- Android:port matches the port in the URL
- Android:path matches the path in the URL
Let's change the Targetactivity declaration information:
<activity android:name= ". Targetactivity ">
<intent-filter>
<action android:name=" Com.scott.intent.action.TARGET "/>
<category android:name= "Android.intent.category.DEFAULT"/>
<data android:scheme= "Scott" Android : host= "Com.scott.intent.data" android:port= "7788" android:path= "/target"/>
</intent-filter>
This time if only the action is specified is not enough, we need to set the data value for it, as follows:
public void gototargetactivity (view view) {
Intent Intent = new Intent ("Com.scott.intent.action.TARGET");
Intent.setdata (Uri.parse ("Scott://com.scott.intent.data:7788/target"));
StartActivity (Intent);
At this point, each part of the URL and the Targetactivity configuration information are all consistent in order to jump successfully, otherwise it will be rejected by the system.
But sometimes it's not good to have a path limit, like we have a URL like this: (Scott://com.scott.intent.data:7788/target/hello) (scott://com.scott.intent.data : 7788/target/hi)
What should we do at this time? We need to use another element: Android:pathprefix, which represents the path prefix.
We modify the android:path= "/target" to android:pathprefix= "/target", and then we can meet the above requirements.
And in the search, we used a Putextra method, the keyword as a parameter placed in the intent, we become extras (additional information), which involves a bundle object.
Bundle and intent have a close relationship, mainly responsible for the intent to save additional parameter information, it implements the Android.os.Paracelable interface, the internal maintenance of a map type of properties, used in the form of key-value pairs to store additional parameter information. When we place additional information using the intent Putextra method, the method checks that the default bundle instance is not null and, if it is empty, creates a new bundle instance, and then places the specific parameter information in the bundle instance. We can also create bundle objects ourselves, and then specify this bundle for intent, as follows:
public void gototargetactivity (view view) {
Intent Intent = new Intent ("Com.scott.intent.action.TARGET");
Bundle Bundle = new Bundle ();
Bundle.putint ("id", 0);
Bundle.putstring ("name", "Scott");
Intent.putextras (bundle);
StartActivity (intent);
}
Note that after you use the Putextras method to set the bundle object, the system is not a reference operation, but a copy operation, so if you change the data in the bundle instance after you set it up, you will not affect additional information within intent. So how do we get additional information that is set in the intent? In this case, we're going to get the bundle instance from the intent, and then we'll take the corresponding key value information out of it:
Bundle Bundle = Intent.getextras ();
int id = bundle.getint ("id");
Of course, we can also use the intent Getintextra and Getstringextra methods to get the data source is the bundle type of instance object in intent.
We've covered three attributes of intent: action, data, and extras. In addition, intent also includes the following properties:
The trait or behavior of a target that performs an action, 3.category.
For example: In our application the main interface activity usually has the following configuration:
Represents the target activity as the initial activity in the task in which the application resides and appears in the application list of the system launcher.
A few common category are as follows:
- Intent.category_default (Android.intent.category.DEFAULT) Default CATEGORY
- Intent.category_preference (Android.intent.category.PREFERENCE) indicates that the target activity is a preference interface;
- Intent.category_ When browsable (Android.intent.category.BROWSABLE) specifies this category, when clicking on a picture or link on a Web page, the system considers the target activity to be included in the optional list for the user to choose to open the picture or link.
When you set up category for intent, you should use the Addcategory (String category) method to add the specified category information to the intent to match the target activity that declares this category.
4.type: MIME data types that can be handled by the target activity to perform the action
For example, a target activity that can process a picture contains such mimetype in its declaration:
When matching using intent, we can use Settype (string type) or Setdataandtype (Uri data, String type) to set the mimetype.
5.component, package or class name of the target component
When using component for matching, the following are generally used:
Intent.setcomponent (New ComponentName (Getapplicationcontext (), Targetactivity.class));
Intent.setcomponent (New ComponentName (Getapplicationcontext (), "com.scott.intent.TargetActivity"));
The first two are used to match targets within the same package, and the third is to match targets within other packages. Note that if we specify the component attribute in intent, the system will no longer match action, Data/type, category.
Let's talk about it today, and then add it if you have time.
Original link: http://blog.csdn.net/liuhe688/article/details/7162988
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.