In the previous study activity has been used many times Intent, when an activity needs to start another activity, the program does not directly tell the system to start which activity, Instead, they express their intentions through intent: which activity needs to be activated.
Intent is not only an "intent" to encapsulate an Android application to start a component, but also an important medium for communication between application components. This section begins to learn about the use of intent.
I. Introduction of Intent
The most common use of intent is to bind application components and communicate between applications. Although Intent can facilitate communication between components in a number of ways, its basic use cases include the following three:
1. Activate Activity
Activity represents a screen in your app. By passing Intent to StartActivity (), you can start a new Activity instance. Intent describes the Activity to be started and carries any necessary data.
You can call Startactivityforresult () if you want the results to be received after the Activity completes. In the activity's Onactivityresult () callback, the activity receives the result as a separate Intent object.
2. Start the service
A Service is a component that performs operations in the background without using the user interface. By passing Intent to StartService (), you can start the service to perform a one-time operation (for example, download a file). Intent describes the service to be started and carries any necessary data.
If the service is designed to use a client-server interface, you can bind to this service from other components by passing Intent to Bindservice ().
3. Send a broadcast
Broadcasts are messages that can be received by any application. Various broadcasts are delivered for system events, such as when the system starts or when the device starts charging. You can pass a broadcast to another app by passing the Intent to Sendbroadcast (), Sendorderedbroadcast (), or sendstickybroadcast ().
Next, a table is used to enumerate the common methods of the intent boot component, as shown in the following table:
In the table above, the common methods for opening different components through intent are listed. It is important to note that there are only two ways to use intent to turn on activity and to open a service, and there are several ways to turn on broadcastreceiver.
Ii. Types of Intent
There are two ways in which intent look for target components in Android, one is explicit intent and the other is implicit intent. The next two types of intent are explained in detail.
1, explicit intent
Explicit intent, that is, when you start activity through intent, you need to explicitly specify the name of the active component. In a program, if you need to start other activity in this app, you can use explicit intent to start the activity, with this example code as follows:
Create a Intent Object
Intentintent=newintent (this, secondactivity.class);
Activate activity
StartActivity (Intent);
In the example code above, the intent object is created by constructing the intent method. The construction method receives two parameters, the first parameter context requires a contexts to start the activity, the second class specifies the target activity to start, and the intent object can be constructed by constructing the method.
In addition to opening components by specifying the class name, explicit intent can also specify an open component based on the package name and full pathname of the target component, as shown in the following code:
Intentintent=newintent ();
Intent.setclassname ("Com.jinyu.cqkxzsxy.android.intent", "com.jinyu.cqkxzsxy.android.intent.SecondActivity");
StartActivity (Intent);
In the example code above, the Setclassname (package name, class full pathname) method specifies that the package name and full path name of the component to be turned on to start another component.
The activity class provides a startactivity (Intent Intent) method that is specifically used to open the activity, which receives a Intent parameter, where a built-in Intent is passed in to launch the target activity.
The activity, which is opened in this way, is so obvious that it is called an explicit intent, also known as an explicit intent.
2. Implicit intent
A intent that does not explicitly specify a component name is called an implicit intent, also known as an implicit intent. The Android system finds the most appropriate component based on the action (action), category, data (URI, and data type) set in the implicit intent. The specific code looks like this:
<activityandroid:name= ". Secondactivity ">
<intent-filter>
<!--set the Action property, you need to open the specified component in your code according to the name you set--
<actionandroid:name= "Com.jinyu.cqkxzsxy.android.intent.action.xxx"/>
<categoryandroid:name= "Android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
In the above-mentioned transport,,<action> indicates that the current activity can respond to "com.jinyu.cqkxzsxy.android.intent.action.xxx", while <category> Tags contain categories of information that can be turned on only when the contents of <action> and <category> match.
The sample code that uses implicit intent to open activity is as follows:
Intentintent=newintent ();
Intent.setaction ("com.jinyu.cqkxzsxy.android.intent.action.xxx");
StartActivity (Intent);
In the above code, intent pinned Setaction ("com.jinyu.cqkxzsxy.android.intent.action.xxx"); This action does not specify category because the configuration in the manifest file " Android.intent.category.DEFAULT "is a default category that automatically adds the category to intent when the StartActivity () method is called.
The implicit intent starts the activity as shown in.
In, Activity A creates a Intent that contains the description of the operation and passes it to StartActivity (). The Android system searches all applications for Intent filters that match Intent. After a match is found, the system initiates a matching activity by invoking the OnCreate () method that matches activity B and passing it to Intent.
In both of these intent, explicit intent must specify the name of the component when it is opened, typically only when the application switches components. The implicit intent function is more powerful than the display intent, not only to open the components of the application, but also to open the components of other applications, such as opening the system's own camera, browser, etc.
Come here today, if you have any questions welcome message to discuss together, also welcome to join the Android 0 Basic introductory Technical discussion group, grow together!
If the series of sharing is helpful to you, move your fingers, like, leave a message, your interaction is the greatest encouragement to me!
This article copyright for the public Share talent show (Shareexpert)--Xin 鱻 all, if necessary reprint please contact the author authorized, hereby declare!
Summary of past period review:
Android 0 Basics Introduction 1th: Android's past life
Android 0 Basics Section 2nd: Android system Architecture and application components those things
Android 0 Basics Section 3rd: Bring you up to talk about Android development environment
Android 0 Basics 4th: Installing and configuring the JDK correctly Ko fu the first trick
Android 0 Basics 5th: Use ADT bundles to easily meet the goddess
Android 0 Basics 6th: Configuration Optimization SDK Manager, official dating goddess
Android 0 Basics 7th: Take care of Android simulator and start the Sweet journey
Android 0 Basics 8th: HelloWorld, the starting point for my first trip
Android 0 Basics 9th: Android app, no code can be developed
Android 0 Basics Section 10th: Development IDE Big upgrade, finally ushered in Android Studio
Android 0 Basics Introductory Section 11th: Simple steps to take you to fly, run Android Studio project
Android 0 Basics 12th: Get familiar with the Android studio interface and start selling
Android 0 Basics 13th: Android Studio Personalized configuration to create a tool for development
Android 0 Basics 14th: Using high-speed genymotion, stepping into the rocket era
Android 0 Basics Section 15th: Mastering the Android Studio project structure, sailing
Android 0 Basics Section 16th: Android User Interface Development overview
Android 0 Basics Section 17th: text box TextView
Android 0 Basics Section 18th: Input box EditText
Android 0 Basics Get started section 19th: Buttons button
Android 0 Basics 20th: check box checkbox and radio button radiobutton
Android 0 Basics Section 21st: Switch Components ToggleButton and switch
Android 0 Basics Section 22nd: Image View ImageView
Android 0 Basics Section 23rd: Image button ImageButton and zoom button Zoombutton
Android 0 Basics Section 24th: Customize view simple to use to create your own controls
Android 0 Basics Section 25th: Simple and most commonly used linearlayout linear layouts
Android 0 Basics Section 26th: Two alignments, layout_gravity and gravity differ greatly
Android 0 Basics section 27th: Using padding and margin correctly
Android 0 Basics Section 28th: Easy to master relativelayout relative layout
Android 0 Basics 29th: Use tablelayout table layouts
Android 0 Basics 30th: Two minutes master Framelayout frame layout
Android 0 Basics Section 31st: absolutelayout absolute Layout with less
Android 0 Basics Section 32nd: New GridLayout Grid layout
Android 0 Basics section 33rd: Android Event Handling overview
Android 0 Basics Section 34th: Listening-based event handling in Android
Android 0 Basics Section 35th: Callback-based event handling in Android
Android 0 Basics Section 36th: Handling of Android system events
Android 0 Basics 37th: First Knowledge ListView
Android 0 Basics 38th: First Knowledge Adapter
Android 0 Basics section 39th: listactivity and custom list items
Android 0 Basics Section 40th: Customizing Arrayadapter
Android 0 Basics Section 41st: Using Simpleadapter
Android 0 Basics Section 42nd: Customizing Baseadapter
Android 0 Basics section 43rd: ListView Optimization and List End-to-end use
Android 0 Basics Section 44th: ListView Data Dynamic Update
Android 0 Basic Getting Started section 45th: Grid view GridView
Android 0 Basics section 46th: List Options Box spinner
Android 0 Basic Getting Started section 47th: AutoComplete text Box Autocompletetextview
Android 0 Basics Section 48th: Collapsible list Expandablelistview
Android 0 Basics section 49th: Adapterviewflipper picture Carousel
Android 0 Basics Section 50th: StackView card Stacking
Android 0 Basics Section 51st: progress bar ProgressBar
Android 0 Basics section 52nd: Customizing ProgressBar Cool progress bar
Android 0 Basics 53rd: Drag bar Seekbar and star rating bar Ratingbar
Android 0 Basics section 54th: View switch Components Viewswitcher
Android 0 Basics Section 55th: Imageswitcher and Textswitcher
Android 0 Basics Section 56th: Flip View Viewflipper
Android 0 Basics Section 57th: DatePicker and Timepicker selectors
Android 0 Basics Section 58th: Value selector numberpicker
Android 0 Basics Section 59th: Common Three clock clocks components
Android 0 Basics Section 60th: Calendar view CalendarView and timer chronometer
Android 0 Basics Section 61st: Scrolling view ScrollView
Android 0 Basics section 62nd: Search box Component Searchview
Android 0 Basics Introductory Section 63rd: Learn from the tab tabhost
Android 0 Basics 64th: Uncover Recyclerview
Android 0 Basics Section 65th: Recyclerview Split Line Development tips
Android 0 Basics Section 66th: Recyclerview Click event Handling
Android 0 Basics Section 67th: Recyclerview Data Dynamic Update
Android 0 Basics 68th: Recyclerview adding end-to-end views
Android 0 Basics Section 69th: Viewpager Quick Implementation Guide page
Android 0 Basics 70th: Viewpager build Tabhost effect
Android 0 Basics 71st: CardView Simple card-based layout
Android 0 Basics Section 72nd: Swiperefreshlayout drop-down refresh
Android 0 Basics Section 73rd: Activity creation and configuration
Android 0 Basics Section 74th: Activity startup and shutdown
Android 0 Basics Section 75th: Activity status and life cycle
Android 0 Basics Section 76th: Activity data saving and screen switching
Android 0 Basics Section 77th: Activity task stacks and startup modes
Android 0 Basics Section 78th: The four components of the link--intent