Activity, intent, intent filter, service, Broadcast, BroadcaseR

Source: Internet
Author: User
Tags home screen

Activity
In Android, Activity is the foundation of all programs, and the processes of all programs run in the Activity. Activity has its own life cycle (the life cycle is controlled by the system, and the program cannot be changed, however, you can use onSaveInstanceState to save its status ).
The key to an Activity is its life cycle (for example, the classic Life Cycle Graph =. =), followed by onSaveInstanceState onRestoreInstanceState, and inter-Activity redirection and data transmission (intent ).

Common functions in Activity include SetContentView () findViewById () finish () startActivity (). The following are the functions involved in the lifecycle:
Void onCreate (Bundle savedInstanceState)
Void onStart ()
Void onRestart ()
Void onResume ()
Void onPause ()
Void onStop ()
Void onDestroy ()
Note that you must add the corresponding <Activity> in the Manifest file and set its attributes and intent-filter to use the Activity.

Intent
Android provides an Intent mechanism to assist in interaction and communication between applications. Intent describes the actions, actions involving data, and additional data of an application, android finds the corresponding component based on the description of the Intent, passes the Intent to the called component, and calls the component. Intent can be used not only between applications, but also between activities/services within the application. Therefore, Intent acts as a media intermediary here, providing information about component calls to each other to decouple callers from callers. The function of Intent is shown in the SDK as follows:

· Start an Activity through Context. startActivity () javastivity. startActivityForResult;

· Start a service through Context. startService () or use Context. bindService () to interact with background services;

· Send the broadcast method (such as Context. sendBroadcast (), Context. sendOrderedBroadcast (), and Context. sendStickyBroadcast () to broadcast receivers.

Intent attribute settings include the following: (The following are definitions in XML, and can also be obtained and set using the Intent class method)
(1) Action, that is, the Action to be executed
The SDk defines some standard actions, including

Onstant
Target component
Action
 
ACTION_CALL
Activity
Initiate a phone call.
 
ACTION_EDIT
Activity
Display data for the user to edit.
 
ACTION_MAIN
Activity
Start up as the initial activity of a task, with no data input and no returned output.
 
ACTION_SYNC
Activity
Synchronize data on a server with data on the mobile device.
 
ACTION_BATTERY_LOW
Broadcast receiver er
A warning that the battery is low.
 
ACTION_HEADSET_PLUG
Broadcast receiver er
A headset has been plugged into the device, or unplugged from it.
 
ACTION_SCREEN_ON
Broadcast receiver er
The screen has been turned on.
 
ACTION_TIMEZONE_CHANGED
Broadcast receiver er
The setting for the time zone has changed.
 


Of course, you can also customize the action (when using a custom action, you need to add the package name as the prefix, such as "com. example. project. SHOW_COLOR), and you can define the corresponding Activity to process our custom actions.
(2) Data, that is, the Data to be operated by the execution action
Android uses a URI pointing to data. For example, in a contact application, a URI pointing to a contact may be: content: // contacts/1. For different actions, the URI Data type is different (you can set the type attribute to specify a specific type of Data). For example, ACTION_EDIT specifies that Data is the file URI and calls tel: URI, the access network is http: URI, while the data provided by the content provider is content: URIs.
(3) type (data type), explicitly specifying the Intent data type (MIME ). Generally, the Intent data type can be determined based on the data itself. However, by setting this attribute, You can forcibly use the explicitly specified type instead of derivation.
(4) category (category): additional information about the executed action. For example, LAUNCHER_CATEGORY indicates that the receiver of Intent should appear as a top-level application in Launcher, while ALTERNATIVE_CATEGORY indicates that the current Intent is one of a series of optional actions that can be executed on the same piece of data. There are other

Constant
Meaning
 
CATEGORY_BROWSABLE
The target activity can be safely invoked by the browser to display data referenced by a link-for example, an image or an e-mail message.
 
CATEGORY_GADGET
The activity can be embedded inside of another activity that hosts gadgets.
 
CATEGORY_HOME
The activity displays the home screen, the first screen the user sees when the device is turned on or when the HOME key is pressed.
 
CATEGORY_LAUNCHER
The activity can be the initial activity of a task and is listed in the top-level application launcher.
 
CATEGORY_PREFERENCE
The target activity is a preference panel.
 


(5) component: Specifies the Class Name of the Intent's target component. Generally, Android searches for other attributes in Intent, such as action, data/type, and category, and finds a matched target component. However, if this attribute of component is specified, the component specified by component will be used directly without executing the above search process. After this attribute is specified, all other Intent attributes are optional.
(6) extras (additional information) is a collection of all other additional information. You can use extras to provide extended information for components. For example, if you want to perform the "send email" action, you can save the email title and body in extras, send to the email sending component.
One of the keys to understanding Intent is to understand the two basic usage of Intent: an explicit Intent, that is, specifying the receiver when constructing the Intent object; the other is an implicit Intent, that is, when the Intent sender constructs an Intent object, it does not know or care about who the receiver is. This helps reduce the coupling between the sender and the receiver.
Android does not need to parse explicit Intent because the target component is clear. Android needs to parse implicit Intent, map Intent to the Activity, IntentReceiver, or Service that can process the Intent.
The Intent parsing mechanism mainly finds the matching Intent by finding all intentfilters registered in AndroidManifest. xml and the Intent defined in them. In this parsing process, Android uses the Intent action, type, and category attributes to determine the attributes. The judgment method is as follows:

· If Intent specifies an action, the action must be included in the action list of the IntentFilter of the target component; otherwise, the action cannot be matched;

· If the Intent does not provide the type, the system will obtain the data type from the data. Like action, the Data Type list of the target component must contain the Intent data type. Otherwise, the data type does not match.

· If the data in the Intent is not a content: type URI and the Intent does not explicitly specify its type, it will match according to the scheme (such as http: or mailto :) of the data in the Intent. Similarly, the scheme of Intent must appear in the scheme list of the target component.

· If Intent specifies one or more category categories, all these categories must appear in the Set category list. For example, Intent contains two categories: LAUNCHER_CATEGORY and ALTERNATIVE_CATEGORY. The parsed target component must contain at least these two categories.

 

 

 

Intent-Filter Definition
Examples of setting attributes:
<Action android: name = "com. example. project. SHOW_CURRENT"/>
<Category android: name = "android. intent. category. DEFAULT"/>
<Data android: mimeType = "video/mpeg" android: scheme = "http".../>
<Data android: mimeType = "image/*"/>
<Data android: scheme = "http" android: type = "video/*"/>
Complete instance
<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>
Intent usage instance
1. No parameter for Activity jump
Intent it = new Intent (Activity. Main. this, Activity2.class );
StartActivity (it );
2. pass data to the next Activity (use Bundle and Intent. putExtras)
Intent it = new Intent (Activity. Main. this, Activity2.class );
Bundle bundle = new Bundle ();
Bundle. putString ("name", "This is from MainActivity! ");
It. putExtras (bundle); // it. putExtra ("test", "shuju ");
StartActivity (it); // startActivityForResult (it, REQUEST_CODE );
You can use the following methods to obtain data:
Bundle bundle = getIntent (). getExtras (); String name = bundle. getString ("name ");
3. Return the result to the previous Activity (use setResult to start the Activity for startActivityForResult (it, REQUEST_CODE)
Intent intent = getIntent ();
Bundle bundle2 = new Bundle ();
Bundle2.putString ("name", "This is from ShowMsg! ");
Intent. putExtras (bundle2 );
SetResult (RESULT_ OK, intent );
4. Call back the result processing function (onActivityResult) of the previous Activity)
@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data ){
// TODO Auto-generated method stub
Super. onActivityResult (requestCode, resultCode, data );
If (requestCode = REQUEST_CODE ){
If (resultCode = RESULT_CANCELED)
SetTitle ("cancle ");
Else if (resultCode = RESULT_ OK ){
String temp = null;
Bundle bundle = data. getExtras ();
If (bundle! = Null) temp = bundle. getString ("name ");
SetTitle (temp );
}
}
}
The following are some other Intent usage examples (

Related Article

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.