An example analysis of the usage of display intention and implicit intention in the intent of Android learning _android

Source: Internet
Author: User

This example describes the use of intent and implicit intent in the intent of Android learning. Share to everyone for your reference, specific as follows:

Intent (Intent) is primarily to address the communications between the various components of the Android application.

Intent is responsible for the operation of the application of the action, action involved in data, additional data description, Android according to this intent description, responsible for finding the corresponding components, the intent passed to the calling component, and complete the component calls.

Therefore, intent plays a role as a media intermediary, which provides information about the invocation of components to each other, and realizes the decoupling between the caller and the callee.

For example, in a contact maintenance application, when we click on a contact in a contact List screen (assuming the corresponding activity is listactivity), we want to be able to jump out of the contact's details screen ( Assuming the corresponding activity is detailactivity)

To do this, listactivity needs to construct a Intent that Intent to tell the system that we want to do a "view" action that corresponds to the viewing object "a contact" and then calls StartActivity (Intent Intent). The constructed intent is passed in, the system will find the activity that satisfies this intent requirement in the manifest according to the description in this intent, the system will call the activity found, that is detailactivity, the final incoming intent, Detailactivity performs the appropriate action according to the description in this intent.

The main classifications for intent include implicit intent and explicit intent. Explicit intent is usually primarily to start data between the activities in this application, while implicit intent is often used to initiate certain actions in the system, such as making a phone call, or initiating an activity across applications.

Generally speaking:

Explicit intent: Calling the Intent.setcomponent () or Intent.setclass () method explicitly specifies the intent of the component name as an explicit intent, explicitly specifying which component intent should pass to.
Implicit intent: Intent that do not explicitly specify the component name are implicit intent. The Android system handles this intent by finding the most appropriate component for the action, category (category), data (URI, and data type) set in the implicit intent.

The display intent is very common and very simple. Generally used for mutual invocation conversions between the activity within the application, it is not much to say, here I introduce an implicit intent:

The simple summary is: The intention includes: action (Actions), Category (additional information), data (data, specific content), Tpye (type) and so on, for example, the intention is to start a component of the complete action information, like hitting, playing is action, The person is the data content, and type is the kind, hit what person? Typing is the type of bad, and only this information is complete to execute a full intent, and of course there are some information, such as scheme is the prefix of the URI type data, like the SMS in this example: the host hostname, path path, and so on.

Here I give you an example of calling system SMS, using implicit intent to start system SMS, and give 10086 examples of sending messages. The code is as follows:

/** 
* Implicit intent method to start the system SMS 
* 
* Simple Summary is: The intention includes: action (Actions), Category (additional information), data (data, specific content), Tpye (type) and so on, for example, 
* The intention is to start a component of the complete action information 
*, like hitting, playing is action action, people is the data content, and type is types, hit what people? Typing is the type of the bad point 
*, only the information is complete to execute a full intent 
*, of course, there are some information, such as scheme is the URI type of data prefix, like this example of the SMS: and host hostname, path path, etc. 
* 
@param view */public 
void Startone (view view) { 
  Intent Intent = new Intent (); 
  Intent.setaction ("Android.intent.action.SENDTO");/the action that sends the message 
  intent.addcategory (" Android.intent.category.DEFAULT ")//additional Information 
  Intent.setdata (Uri.parse (" sms:10086 "))//specific data, sent to 10086 
  StartActivity (intent); 
}

The above example is the call to the system, and let's take a look at how the custom implicit intent is invoked.
The first is to register in the manifest file, the code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Net.loonggg.intent "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk Andro id:minsdkversion= "8" android:targetsdkversion= "/>" <application android:allowbackup= "true" Android:ico 
   n= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity Android:name= "net.loonggg.intent.MainActivity" android:label= "@string/app_name" > <intent-filter> &L T;action android:name= "Android.intent.action.MAIN"/> <category android:name= " Android.intent.category.LAUNCHER "/> </intent-filter> </activity> <activity android:name=" net. Loonggg.intent.SecondActivity "> <intent-filter> <!--Custom Actions--> <action android:name=" net . loonggg.xxx "/> <!--custom scheme and host--> <data 
     Android:host= "www.baidu.com" android:path= "/person" android:scheme= "Loonggg"/> <!--custom type --> <data android:mimetype= "Person/people"/> <!--additional information--> <category "android:name= Id.intent.category.DEFAULT "/> </intent-filter> </activity> </application> </manifest&gt
 ;

The thing in the second activity is our custom setting, which is the code below:

<activity android:name= "net.loonggg.intent.SecondActivity" > 
 <intent-filter> 
  <!--customized actions > 
  <action android:name= "net.loonggg.xxx"/> 
  <!--custom scheme and host--> 
  <data 
   Android:host= "www.baidu.com" 
   android:path= "/person" android:scheme= "loonggg"/> 
  <!--custom Type-- > 
  <data android:mimetype= "Person/people"/> 
  <!--additional information--> <category 
  " Android.intent.category.DEFAULT "/> 
 </intent-filter> 
</activity>

How do you invoke customizations in your code? In fact, like the call system SMS, just call the system text messages in those actions and data, the system has been configured for us, we directly use the line, and custom is our own write, call methods and systems are actually the same. The method is as follows:

/** 
* 
@param view 
/public void Starttwo (view view) { 
  Intent Intent = new Inten by custom implicit intent T (); 
  Intent.setaction ("net.loonggg.xxx"); 
  Intent.addcategory ("Android.intent.category.DEFAULT"); 
  Intent.setdataandtype (Uri.parse ("Loonggg://www.baidu.com/person"), 
    "Person/people"); 
  StartActivity (intent); 
}

A special note: Intent.setdata (data) and Intent.settype (type) Note that the two methods will clear each other, meaning that if you set the SetData (data) before setting SetType (type), Then the set of Settype (type) will remove the contents of the previous SetData (data), and the error will be the same, so if you have to set the type and data, then use the Setdataandtype (Data,type) method.

I hope this article will help you with the Android program.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.