Use of implicit intent--android Learning Note 3

Source: Internet
Author: User

use of implicit intent

First, why use implicit intent?


However, if you want to invoke the components of another program, and the developer often does not know the component name of the other application, then we can only use implicit intent, implicit intent the contrary, it does not use the component name to define the target component to be activated, Instead, the Android system helps the application find the component that best matches the intent request intent.



Second, how to find the Android system?


mainly through intent filter to find objects related to the implicit intent. The specific choice is: Android will intent the request content <intent-filter> comparison, intent filter contains all the possible components to be selected in the system. If a component in <intent-filter> matches the content of an implicit intent request, then Android chooses the component as the target component of the implicit intent.



The basic format of <intent-filter> is as follows:

<!--1. The target activity (target component) is defined here. secondactivity--><!--2. Indicate that the activity's action ID action is:com.example.activity.action_start--><!--3. Indicate the activity " Category ID ": Android.intent.category.DEFAULT and--><!--com.example.activity.CPJ_ Category1 and Com.example.activity.cpj_category2--><activity android:name= " Com.example.activity.SecondActivity "><intent-filter><action android:name=" Com.example.activity.ACTION_START "/> <category android:name=" Android.intent.category.DEFAULT "/>< Category android:name= "Com.example.activity.CPJ_category1"/><category android:name= " Com.example.activity.CPJ_category2 "/></intent-filter></activity><!-- Specifies that the current activity can respond Com.example.activity.ACTION_START this action--><!--the 1.<category> tag contains some additional information, More precisely indicates that the current activity can respond to intent, and may also have a category--><!--2. Only action and category match to respond intnet--> <!--3.andro         Id.intent.category.DEFAULT is a default category that automatically adds this category to intent when calling--><!--startactivity ().           <!--4. A intent can have only one action, but multiple category--><!--5. A intent-filter can have more than one action--> 



Iv. How does the intent request content compare with <intent-filter>?


1. Action comparison-<action/> (first step) such as:
<intent-filter><actionandroid:name= "Com.example.project.SHOW_CURRENT"/><actionandroid:name= " Com.example.project.SHOW_RECENT "/><actionandroid:name=" Com.example.project.SHOW_PENDING "/></ Intent-filter>

1). A <intent-filter> element should contain at least one <action>, otherwise no intent request can match the <intent-filter>.
  
2). If the action requested by intent and the <intent-filter> one <action> match, then the intent is compared by this <intent-filter> action. (First step completed)


2. Category comparison-<category/> (second step) such as:
<intent-filter><categoryandroid:name= "Android. Intent.Category.DEFAULT "/><categoryandroid:name=" Android. Intent.Category.BROWSABLE "/></intent-filter>

1). The match succeeds only if all of the category in the intent request exactly matches the <category> of one of the intent-filter in the component. It is also said that a category in a <intent-filter> must contain all the categories in intent before the class match succeeds. (Second step completed)
  
2). Redundant <category> declarations in Intent-filter do not result in matching failures, which means that the category in <intent-filter> can have extra items;

3). <categoryandroid:name= "Android. Intent.Category.DEFAULT "/> is a default category that automatically adds the category to Intent when calling StartActivity ().

4). Each implicit Intent issued by the StartActivity () method has at least one category, which is "Android.intent.category.DEFAULT", So as long as the activity that wants to receive an implicit intent should include
"Android.intent.category.DEFAULT" category, otherwise it will cause the intent match to fail.

3. Data comparison-<data/> (third step) such as:
<intent-filter><data android:type= "video/mpeg" android:scheme= "http" .../><data android:type= " Audio/mpeg "android:scheme=" http ".../></intent-filter>

1). The <data> element specifies the data URI and data type of the intent request that you want to accept, and the URI is divided into three parts to match: scheme, authority, and path. The URI data type and scheme of the intent request set with SetData () must be the same as specified in Intent-filter. If authority or path are also specified in the Intent-filter, they also need to be matched before passing the test. (Completion of the third step)

Total:
Only the first step match will continue to match the category and the second step of this <intent-filter>, and the second match is completed before the third step is matched. When all match up, find the corresponding <activity android:name= that contains them. Secondactivity "> Jump to the corresponding target component.



V. Common uses of intent.action (built-in actions for Android):



1. Intent.action_main

String:android.intent.action.MAIN
Function: Identifies the activity as the start of a program (indicates the activity that the app performs first). More commonly used.
Input:nothing
Output:nothing

Cases:

<!--Android.intent.action.MAIN  determines the activity--><!--the application starts first Android.intent.category.LAUNCHER  Determines whether the application appears in the list of programs--><activity android:name= ". Main "android:label=" @string/app_name ">   <intent-filter><action android:name=" Android.intent.action.MAIN "/><category android:name=" Android.intent.category.LAUNCHER "/></ Intent-filter></activity>

Example: Using intent to implement the home desktop of the return system
Intent Intent = new Intent ();  Intent.setaction (intent.action_main);  Intent.addcategory (Intent.category_home); startactivity (intent);


2. Intent.action_call


Stirng:android.intent.action.CALL
Function: Call the specified phone number (call directly).
Input: Phone number. The data format is: Tel:+phone number
Output:nothing

Example:
Intent intent=new Intent ();   Intent.setaction (Intent.action_call);//Set current action for Call     Intent.setdata (Uri.parse ("tel:1320010001"));// Set the phone startactivity (intent) to be dialed;

in the Androidmainifest.xml file, set the call permissions for the current program:
<uses-permission android:name= "Android.permission.CALL_PHONE"/>


3. Intent.Action.DIAL


String:action.intent.action.DIAL
Function: Call dial panel (just call, and do not start to call)
Input: Phone number. The data format is: Tel:+phone number
Output:nothing
Description: Opens the dial-up UI for Android. If no data is set, an empty UI is opened;

Example:
Intent Intent = new Intent ();  Intent.setaction (intent.action_dial);//Set the current action to call the dial pad  intent.setdata (uri.parse ("tel:1584014xxxx"));// Set up the phone startactivity (intent) to be dialed;

(Here's an example of how to make a phone call, including: Direct dialing, open dial, user-defined dialing)
Button's Click event-Direct call button Btn_call = (Button) Findviewbyid (R.id.call); Btn_call.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (); Intent.setaction ( Intent.action_call);//Set the current action to call Intent.setdata (Uri.parse ("tel:1584014xxxx")),//Set the phone to be dialed startactivity (Intent);}); /button Click Event-Direct call button Btn_dial = (Button) Findviewbyid (r.id.dial); Btn_dial.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (); Intent.setaction ( intent.action_dial);//Set the current action to call the dial pad intent.setdata (uri.parse ("tel:1584014xxxx"));//Set up the phone to be dialed startactivity ( Intent);}); /get user input phone number edittextfinal EditText edt_phonenumber = (EditText) Findviewbyid (r.id.phonebunber_id);// button click Event-user dials Button Btn_usercall = (Button) Findviewbyid (R.id.usercall); Btn_usercall.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//First verify the validity of the user input phone (using the package from Android to determine the validity of the phone) String PhoneNumber = Edt_phonenumbeR.gettext (). toString (); if (Phonenumberutils.isglobalphonenumber (PhoneNumber)) {Intent intentdial = new Intent (); Intentdial.setaction (Intent.action_call);//Set Current user-defined dial Intentdial.setdata (Uri.parse ("Tel:" + phonenumber); StartActivity (intentdial);//Prompt user for wrong phone input} else {Toast.maketext (Callactivity.this, "you entered the wrong phone, please re-enter", Toast.length_ Short). Show ();}});




4. Intent.action_view


String:android.intent.action.VIEW
Function: Used to display the user's data, more general, according to the user's data type to open the corresponding activity.
Data type means: Tel (open dial activity),http (Open browser activity),Geo (open map targeting activity), etc.

Description: Here is a demonstration of several simple applications;

Button's Click event-Open Map button Btn_geo = (button) Findviewbyid (R.id.geo); Btn_geo.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (); Intent.setaction ( Intent.action_view);//Set the current action Intent.setdata (Uri.parse ("geo:39.899533,116.036476"));//Open map location (directly open the map software on your phone) StartActivity (intent);}}); /button Click Event-Open Browser Button btn_http = (button) Findviewbyid (r.id.http); Btn_http.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (); Intent.setaction ( Intent.action_view)///Set Current action Intent.setdata (Uri.parse ("http://www.baidu.com"));//Open browser and open Baidu StartActivity (Intent );}});/ /button Click Event-Opens a picture Button Btn_image = (button) Findviewbyid (r.id.image); Btn_image.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (); Intent.setaction ( Intent.action_view)///Set current action//If you do not know your storage path, you can view the URI uri under Ddms = Uri.parse ("File:///storage/sdcard1/DCIM/Camera/IMG_ 20150622_172748.jpg "); Intent.setdataandtype (URI," image/* "); StartActivity (intent);}); /button Click Event-Open video Button btn_video = (button) Findviewbyid (R.id.video); Btn_video.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent (); Intent.setaction ( Intent.action_view);//Set the current action URI uri = uri.parse ("file:///storage/sdcard1/---"); Intent.setdataandtype (URI, "video/* "); StartActivity (intent);}}); Register event for button-View Address Book button SEECONTACTS_BT = (button) Findviewbyid (r.id.seecontacts); Seecontacts_bt.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new  Intent ();  Intent.setaction (Intent.action_view); Intent.setdata (Uri.parse ("CONTENT://CONTACTS/PEOPLE/1"));//view the first contact in the Address Book startactivity (intent);});



5. Intent.action_sendto


String:android.intent.action.SENDTO
Role: Send SMS
Description: Here is a demonstration of several simple applications;

Example:
String number = Numberet.gettext (). ToString ();//Gets the user input string message = Messageet.gettext (). ToString ();// Get the user input text message if (Phonenumberutils.isglobalphonenumber (number)) {Intent Intent = new Intent ();//Create Intent Object Intent.setdata ( Uri.parse ("Smsto:" + number); Set the number to be sent Intent.putextra ("Sms_body", message); Set the message content to be sent startactivity (intent),//Pass intent to activity} else {Toast.maketext (sendtoactivity.this, "The phone you entered is wrong, please re-enter!) ", Toast.length_short). Show ();}

(There are 2 ways to send SMS to Android:
The First kind: Is the above write the system to send the text message function;
In this way, when you click the Send button, and do not immediately send out, but let the system open your phone's own texting program,
and write the phone number and text message you entered on the above, you click on the phone's own SMS program send button, to send out,
Just like the front of the action_dial, only opened the dial pad, and did not start to call;
The second kind: Use Smsmanager, call system SMS interface to send SMS directly;
In this way, when you click on the Send Text message button you write, you send it directly, and do not call other SMS programs.
And this way can monitor the sending status and the receiver status (specific later introduction))




6. Intent.action_battery_low

Action: Displays a low battery warning message
Explanation: Because this piece uses the broadcast, therefore the concrete realization method in the review about the broadcast knowledge to write



7. Intent.action_edit

Role: Edit a specific contact information in the Address Book

Cases:

Intent Intent = new Intent ();  Intent.setaction (intent.action_edit);  Intent.setdata (Uri.parse ("CONTENT://CONTACTS/PEOPLE/1"));//Modify the first contact in the Address Book startactivity (intent);

 
(There is another way to get all the contact information in the correspondence-use the content provider, later)


Reference website and information:


1.http://blog.csdn.net/coder80/article/details/7879259
2.http://www.cnblogs.com/hanyonglu/archive/2012/03/26/2417278.html
3. "Android Program Development Example Treasure"












Use of implicit intent--android Learning Note 3

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.