Android_intent intent of the explanation

Source: Internet
Author: User
Tags gettext response code

Intent is an abstract description of the action to be performed by intent to assist in the completion of communication between the various components of Android. For example, invoke the activity instantiation object's startactivity () to initiate an activity, or broadcaseintent () to all interested broadcasereceiver, or by StartService ()/bindservice () to start a backend service. As can be seen, intent is primarily used to initiate activity or service (and carry parameter information that needs to be passed), intent to understand the binder between activity.
In short, intent has the ability to activate components and carry data! 2.Intent form (1). Display (Explicit Intents)         Explicitly specify intent of the component name as explicit intent, specifying that the intent should be passed to that component. By following the code, you can create a materialized object and set the parameter information that needs to be passed. Because the specific component object is explicitly specified, it is not necessary to set other intent filtering objects for intent. [Java]  // 1. Creating Intent instantiation objects    intent Intent = new Intent ();  intent.setclass (Context packagecontext, class<?> CLS);          //Internal call SetComponent (componentname)  intent.setclassname (Context Packagecontext , String ClassName); Internal call SetComponent (componentname)  intent.setclassname (string PackageName, string className);    /Internal call setcomponent (componentname), you can activate the external application    intent.setcomponent (new ComponentName ( This, class<?> CLS));  intent.setcomponent (New ComponentName (This, "Package Name"));   (2). Implicit intent (implicit Intents) does not explicitly specify intent of the component name as implicit intent, the system will match the most appropriate component based on the action (action), category, data URI, etc. set in the implicit intent. 1). Actionthe general action to be performed, such as Action_view, Action_edit, Action_main, includingAndroid system-specified and custom [Java]  intent.setaction ("Com.baidu.action.TEST");  [html] View plaincopy<action android:name= "Com.baidu.action.TEST"/>  2). dataexpressed as a Uri, the Data to operate on, such as a person record in the contacts database. The action of the system comes with a simple example action Data (Uri) Content Action_viewcontent://contacts/people/1display information about the person whose identifier is "1". Action_viewtel:123display the Phone Dialer with the given number filled in. Action_dialtel:123display the Phone Dialer with the given number filled in.  custom data matching [Java]  intent.setdata ( Uri.parse ("Baidu://www.baidu.com/news"));  [html]  <!--Android:path content strings need to be/start with-- <data android:scheme= "Baidu" android:host= " Www.baidu.com "android:path="/news "/>  3). Categorygives additional information about the action to Execute. Note: The Android.intent.category.DEFAULT category must be specified in the project manifest's XML file Intent filter, activities will very often need to the Category_default So, they can be found by context.startactivity,or Context can ' t the acitivity Component[java]  i Ntent.addcategory ("Com.baidu.category.TEST");  [html] <!--must specify Category_default, only so startactivity (intent) can find- <category android: Name= "Com.baidu.category.TEST"/>  <categorY android:name= "Android.intent.category.DEFAULT"/>   In addition to the main attributes above, there are additional properties that can be enhanced. 4). TypeSpecifies an explicit type (a MIME type) of the intent data. [Java]  intent.settype ("Image/jpeg");  [html] View plaincopy<data android:mimetype= "image/*"/>   Note: The data Uri and type in the Java file cannot be set using their respective functions at the same time. Because the Uri is cleared when you use the type, you can use the Setdataandtype method to set [Java] intent.setdataandtype (Uri.parse ("baidu://www.baidu.com/ News ")," image/jpeg ");  [html]  <data android:mimetype= "image/*" Android:scheme= "Baidu" android:host= "Www.baidu.com" Android: Path= "/news"/>   (3). The use of the difference between the two uses   explicit intent is generally used internally in the application, because the name of the component is already known within the application, and can be called directly. When an app activates activity in another app, it can only use implicit intent to create an intent based on the intent filter configured by the activity so that the values of each parameter in the intent match the filter so that activity in the other app can be activated. Therefore, implicit intent is used between the application and the application. 3.Activity Intent data transfer [Java] data transfer between  //activity  // 1. Pass in a key-value pair directly to the intent object (equivalent to a intent object with a map key-value pair function)  intent.putextra ("First", Text1.gettext (). toString ());  intent.putextra ("Second", Text2.gettext (). toString ());     // 2. Create a new Bundle object, add a key-value pair to the object, and then add the object to the intent  bundle bundle = new Bundle ();  bundle.putstring ("First", "Zhang");  bundle.putint ("Age", 20);  intent.putextras (bundle);    // 3. Adding ArrayList Collection objects to intent  intent.putintegerarraylistextra (name, value);  intent.putintegerarraylistextra (name, value);       // 4.intent Pass an Object object (the class of the object being passed implements the Parcelable interface, or implements the Serialiable interface)  public Intent PutExtra (string name, Serializable value)  public Intent PutExtra (string name, parcelable value)    4.Activity return result of Exit [Java]  // 1. Start an activity by Startactivityforresult mode   MainActivity.this.startActivityForResult (Intent, 200); The  //intent object, and the  requestcode request code    // 2. The new activity sets the Setresult method that can be passed Responsecode and Intent Object  setresult (101, Intent2);                               &NBSP//responsecode response code and intent object    // 3. Overwrite Mainactivity method in Onactivityresult, once the new activity exits, The method is executed  protected void onactivityresult (int requestcode, int resultcode, Intent data) {     toast.ma Ketext (This, Data.getstringextra ("info") + "Requestcode:" +requestcode+ "ResultCode:" +resultcode, Toast.length_long) . Show ();  }  5.intent Common Applications (EXT) (1). Call Dialer [Java] uri Uri = Uri.parse ("tel:10086");   intent Intent = new Intent (intent.action_dial, URI);   startactivity (Intent);    (2). Send SMS or MMS [java]  //sms  uri Uri = Uri.parse ("smsto:10086");   intent Intent = new Intent (intent.action_sendto, URI);   intent.putextra ("Sms_body", "Hello");   startactivity (Intent);     //send MMS, equivalent to send SMS with attachments  intent Intent = new Intent (intent.action_send);   intent.putextra ("Sms_body", "Hello");   uri Uri = Uri.parse ("Content://media/external/images/media/23"); &nBsp; intent.putextra (Intent.extra_stream, URI);   intent.settype ("Image/png");   startactivity (Intent);    (3). Open Web page via browser [java]  uri Uri = Uri.parse ("http://www.google.com");   intent Intent  = New Intent (Intent.action_view, URI);   startactivity (Intent);   (4). Send e-mail [java]  uri Uri = Uri.parse ("mailto:[email protected]");   intent Intent = new Intent (intent.action_sendto, URI);   startactivity (Intent);     //to [email protected] e-mail send content "Hello" mail   intent Intent = new Intent ( Intent.action_send);   intent.putextra (Intent.extra_email, "[email protected]");   intent.putextra (Intent.extra_subject, "SUBJECT");   intent.putextra (Intent.extra_text, "Hello");   intent.settype ("Text/plain");   startactivity (Intent);     //to many people e-mail   intent intent=new Intent (intent.action_send);    string[] tos = {"[email protected]", "[email protected]"}; Recipient   string[] CCS = {"[email protected]", "[email protected]"}; CC   string[] BCCs = {"[email protected]", "[email protected]"}; Secret Delivery   intent.putextra (Intent.extra_email, TOS);   intent.putextra (INTENT.EXTRA_CC, CCS);   intent.putextra (INTENT.EXTRA_BCC, BCCs);   intent.putextra (Intent.extra_subject, "SUBJECT");   intent.putextra (Intent.extra_text, "Hello");   intent.settype ("message/rfc822");   startactivity (Intent);    (5). Show map and path planning [Java]  //open Google Maps China Beijing location (latitude 39.9, longitude 116.3)   uri Uri = Uri.parse ("Geo : 39.9,116.3 ");   intent Intent = new Intent (Intent.action_view, URI);   startactivity (Intent);     //path planning: From Somewhere in Beijing (latitude 39.9, longitude 116.3) to a place in Shanghai (north latitude 31.2, longitude 121.4)   uri Uri = Uri.parse ("http:// maps.google.com/maps?f=d&saddr=39.9 116.3&daddr=31.2 121.4 ");   intent Intent = new Intent (Intent.action_view, URI);   startactivity (Intent);    (6). Play Multimedia [java intent Intent = new Intent (intent.action_view);   uri Uri = Uri.parse ("File :///sdcard/foo.mp3 ");   intent.setdataandtype (URI, "Audio/mp3");   startactivity (Intent);     uri Uri = Uri.withappendedpath (MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");   intent Intent = new Intent (Intent.action_view, URI);   startactivity (Intent);    (7). Take photos [Java]  //Open the camera program   intent Intent = new Intent (mediastore.action_image_capture);    startactivityforresult (Intent, 0);     //take out the photo data   bundle extras = Intent.getextras ();    bitmap Bitmap = (Bitmap) extras.get ("Data");    (8). Get and cut pictures [Java]  //get and cut pictures   intent Intent = new Intent (intent.action_get_content);   intent.settypE ("image/*");   intent.putextra ("Crop", "true"); Open Shear   intent.putextra ("Aspectx", 1); The width-to-height ratio of the cut is 1:2   intent.putextra ("Aspecty", 2);   intent.putextra ("Outputx", 20); Save picture width and height   intent.putextra ("outputy", 40);    intent.putextra ("Output", Uri.fromfile (New File ("/mnt/sdcard/temp")); Save path   intent.putextra ("OutputFormat", "JPEG");//return format   startactivityforresult (intent, 0);     //cut a specific picture   intent Intent = new Intent ("Com.android.camera.action.CROP");    intent.setclassname ("Com.android.camera", "com.android.camera.CropImage");    intent.setdata (Uri.fromfile ("New File") ("/mnt/sdcard/temp"));    intent.putextra ("Outputx", 1); The width-to-height ratio of the cut is 1:2   intent.putextra ("Outputy", 2);   intent.putextra ("Aspectx", 20); Save picture width and height   intent.putextra ("aspecty", 40);   intent.putextra ("scale", true);   intent.pUtextra ("Nofacedetection", true);    intent.putextra ("Output", Uri.parse ("file:///mnt/sdcard/temp"));    startactivityforresult (Intent, 0);    (9). Open Google Market[java]  //open Google market directly into the program's detailed page   uri Uri = Uri.parse ("market:// Details?id= "+" Com.demo.app ");   intent Intent = new Intent (Intent.action_view, URI);   startactivity (Intent);    (10). Install and uninstall program [Java]  uri Uri = uri.fromparts ("package", "Com.demo.app", null);     intent Intent = new Intent (Intent.action_delete, URI);     startactivity (intent);    (11). Enter the Setup interface [java] //Enter the wireless network settings interface (others can extrapolate)     intent Intent = new Intent ( Android.provider.Settings.ACTION_WIRELESS_SETTINGS);     startactivityforresult (Intent, 0)

Android_intent Intent detailed

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.