Self-study Android notes-using intent in activity

Source: Internet
Author: User

I. Intent Introduction:

Intent's Chinese meaning is "intention, intention", in Android provides the intent mechanism to assist the interaction between the application and communication, intent is responsible for the application of the action, the action involved in data, additional data description, Android according to this intent description, Responsible for locating the corresponding component, passing the intent to the calling component, and completing the call to the component. Intent can be used not only between applications, but also between Activity/service within an application. As a result, intent can be understood as a "medium" for communication between different components, specifically providing information about what components call each other.

Inten How to start a component

Intent can start an activity, start a service, and initiate a broadcast broadcasts. Here's how:

Component Name

Method name

Activity

Startactvity ()

StartActivity ()

Service

StartService ()

Bindservice ()

Broadcasts

Sendbroadcasts ()

Sendorderedbroadcasts ()

Sendstickybroadcasts ()


Two. Explicit intent andImplicit Intent:

The main classifications for intent include implicit intent and explicit intent. Explicit intent is typically primarily to initiate data between activities in the application, whereas implicit intent is common in initiating certain actions in the system, such as making a call, or starting an activity across the app.

explicit Intent: Invoke intent.setcomponent The () or intent.setclass() method explicitly specifies that the Intent of the component name is explicit intent and explicitly intends to explicitly specify the which component the Intent should be passed to.

Implicit intent: Intent with no explicitly specified component name is implicitly intended. The Android system finds the most appropriate component to handle this intent based on the action (action), category, data (URI, and data type) set in the implicit intent.

three. Case-Open System camera:

Activity_main.xml:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= ". Opencamera" >    <textview        android:text= "@string/hello_world"        android: Layout_width= "Wrap_content"        android:layout_height= "wrap_content"/>    <button        android:id= "@+id/ Opencamera "        android:layout_width=" wrap_content "        android:layout_height=" Wrap_content "        android: Layout_centerhorizontal= "true"        android:layout_centervertical= "true"        android:text= "open Camera"/></ Relativelayout>

Main:

Package Com.example.wanglaoda.opencamera;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.widget.button;public class Opencamera extends Activity {@Override protected void onCreate (Bundle savedinsta        Ncestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_opencamera);        Button button = (button) Findviewbyid (R.id.opencamera); Button.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {Intent inte                NT = new Intent ();                Intent.setaction ("Android.media.action.IMAGE_CAPTURE");                Intent.addcategory ("Android.intent.category.DEFAULT");            StartActivity (Intent);    }        }); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio n Bar if it is PresenT. getmenuinflater (). Inflate (R.menu.menu_opencamera, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Self-study Android notes-using intent in activity

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.