Chapter Content
Intent detailed
Calling System programs
Using intent to implement data transfer between components
Custom Intent
Intent Detailed
Intent is a runtime binding (Run-time binding) mechanism that connects two different components during a program's run . With intent, your program can express some kind of request or intention to Android, and Android will select the appropriate component to complete the request based on the intended content.
Android's three basic components-Activity,service and broadcast receiver--are all activated by the intent mechanism, different types of components have different delivery intent way . To activate a new activity, or to have an existing activity do a new operation, you can call the Context.startactivity () or Activity.startactivityforresult () method, To start a new service, or to pass a new instruction to an existing service, call the Context.startservice () method or call Context.bindservice () Method will call the context object of this method with the service binding.
Intent is called intent, which describes the actions , parameters, and additional data of an operation, and is used in many places to understand hyperlinks similar to Web pages.
< Span style= "color: #C00000"? View a contact's profile Send an email to someone Call someone android
based on
Intent
Find the appropriate component and pass in
Intent Execute intent can be passed in
when the following action occurs
content.startactivity ()
? Content.startservice ()
? Content.sendbroadcast ()
components in Android need to be registered in Androidmanifest.xml to be called ? Registering with Intent-filter
the composition of intent, in order to transfer data between different activity, it is necessary to include the corresponding content in the intent, in general, the most basic data should include:
? action: used to indicate what action to implement, such as Action_view, Action_edit, etc.
? Data : a specific figure of fact, usually represented by a URI variable.
? Category: A string that contains information about the kind of component that handles the intent. A intent object can have any category. The intent class defines many category constants
? Type: explicitly specifies the data type of the intent (MIME)
? Component: Specifies the class name of the target component of the intent
? Extras: Additional information
Several common actions are as follows:
Name |
Describe |
Action_call activity |
Start a phone call |
Action_edit activity |
Show user-edited data |
Action_main activity |
Starts as the first activity in a task |
Action_sync activity |
Synchronizing data on the phone with the data server |
Action_battery_low Broadcast Receiver |
Battery charge Low Warning |
Action_headset_plug Broadcast Receiver |
Plug and Unplug headset warning |
ACTION_SCREEN_ON Broadcast Receiver |
Screen Lighten warning |
Action_timezone_changed Broadcast Receiver |
Change Time zone warning |
The Data property is used to match the actionproperty values are usually URI-formatted stringsThe content of the data attribute for different actions varies
? Action_call The corresponding Data is usually " Tel: "starts with
? Action_view corresponding to the Data is usually " http: "starts with
the value of the Data property can be set by the following method
? SetData () can only set URI
? SetType () can only set MIME Type
? Setdataandtype () can set the URI or mime Type
Category describes the extension type information for the target component you can set any descriptionthe Intent class defines several category constants
? Category_browsable
? Categroy_home
? Category_launcher
the value of the Category property can be set by the following method
? Addcategory ()
? Removecategory ()
Extras data for expression of key-value pairs
? You can set many pairs of key-value pairs arbitrarily
? This property has nothing to do with Android match intent
? Intent provides a number of relative get ... Methods and set ... Method for reading and writing data
? When passed into the component, the data can be obtained through the getextras of the bundle
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >//send Mmsuri uri = Uri.parse ("Content://media/external/images/media/23"); Intent it = new Intent (intent.action_send) ; It.putextra ("Sms_body", "some text"); It.putextra (Intent.extra_stream, URI); It.settype ("image/png"); startactivity (IT);</span>
there are two forms of the use of intent
Show Intent: explicitly defines the name of the target component. Notifies the app to launch the corresponding component by specifying a specific component class.
Implicit Intent: The intent of the component Name property is not specified. Match by attributes and Intent-filter. How the intent works
1 , caller-generated Intent object, and set related properties
2. Submit intent request to Android
3, Android to intent analysis, find the corresponding component execution
? You can match multiple components here? Android selects three features when matched by Intent-filterActionDataCategoryall components are registered through the configuration in Androidmanifest.xml? A component that does not register Intent-filter can only respond to explicit intent requests? To assert that Intent-filter can respond to an explicit request or respond to an implicit requestinvoking system objects using intent
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >uri Uri = Uri.parse ("http://www.google.com"); Intent It = new Intent (Intent.action_view,uri); </span>
Show Map
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >uri Uri = Uri.parse ("geo:38.899533,-77.036476"); Intent it = new Intent (Intent.action_view,uri); </span>
calling a dial-up program
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >uri Uri = Uri.parse ("tel:xxxxxx"); Intent it = new Intent (Intent.action_dial,uri); </span>
Note: Calls need to be given permissions in the configuration file
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" ><uses-permission android:name= "Android.permission.CALL_PHONE"/> </span>
using intent for data transferWhen you start a component, you need to pass the boot information through intentintent is a medium that can be used to transmit data.
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >intent it = new Intent (this, settingactivity.class); It.putextra ("username", "root"); StartActivity (it); </ Span>
In the component being launched, the intent object can be obtained through the Getintent method
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >intent it = getintent (); String username = It.getextras (). GetString ("username");</span>
Receive Data returnat the end of the component being started, the data needs to be returned to the caller, and the activity is initiated through Activity.startactivityforresult
Public void Startactivityforresult (Intent Intent, int requestcode)? Parameter Intent description of intent to initiate activity? The parameter requestcode is the request code that is used to identify the callback at the callbackTo set the return information via Activity.setresult
Public void Setresult (int resultcode, Intent data)
? The parameter resultcode is the result encoding of the returned result, used to identify the result type? Parameter data is the returnedReceive Data returnInitiator 's Startup invocation example
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >startactivityforresult (Intent, Request_code);</span>
Initiator's callback receive example
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >protected void Onactivityresult (int requestcode, int resultcode, Intent data) { if (Requestcode = = Request_code) { if (ResultCode = = RESULT_OK) { Data.getextras (). getString ("Someresult") ; }}} </span>
Receive Data returnexample of a callback result set by the initiator
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:14PX;" >bundle bundle = new Bundle (), bundle.putstring ("Someresult", "somevalue"), Bundle.putstring ("Otherresult", " Othervalue "); Intent it = new Intent (); It.putextra (bundle); Setresult (RESULT_OK, it); Finish ();</span>
Custom Intent and Intent-filter
when you need to start an activity in another custom project
? Because the activity of another project is not part of the current project, you cannot get the class
? So we want to be able to start with an implicit intent approach.
? In this case, you need to customize the properties such as action
? and use these custom attributes in intent
Android is a custom requirement that allows intent
? First, use Intent-filter to customize various properties in Androidmanifest.xml
? Then, when you start the activity, you use the custom property in intent
Androidui Advanced 16 use intent for communication