Concept and application of intent in Android (i)--show intent and implicit intent

Source: Internet
Author: User

intent two ways to find a target component:

Explicit Intent: implemented by specifying the name of the intent component, which is typically implemented within the same application, with the knowledge of the target component name.

Implicit intent: implemented by the intent filter, which is generally used for different applications without explicitly stating the name of the target component.

First, display intent:

The complete process of creating an activity:

1. create a class manually to inherit from the activity:

  Public class Myaty extends Activity

2. Let it bind a view:

public class Myaty extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.myaty);

}
}

3, Androidmainfest.xml file for intent configuration:

<activity android:name= ". Myaty "/> <!--<activity android:name=" Com.w.learnintent.myaty "/>-->

4, start the function code:

  activity_main.xml file:<button android:text= "Start myaty" android:id= "@+id/btnstartmyaty"/>

  In the Mainactivity.java file:

Findviewbyid (R.id.btnstartmyaty). Setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
StartActivity (New Intent (Mainactivity.this,myaty.class)); Show intent
}

Second, implicit intent:

1. androidmainfest.xml file for intent configuration:

<activity android:name= ". Myaty ">
<intent-filter>
<category android:name= "Android.intent.category.DEFAULT"/>
<action android:name= "Com.w.learnintent.intent.action.myaty"/>
</intent-filter>
</activity>

2. Create a string action in the Activity--myaty.java that is started:

  public static final String ACTION = "Com.w.learnintent.intent.action.myaty";

3, start the function code:

  StartActivity (New Intent (myaty.action)); Implicit intent

Iii. between different applications (implicit intent):

1. Create another program by creating a new module on the basis of the original project.

2, the original project androidmainfest.xml file for intent configuration:

<activity android:name= ". Myaty "android:exported=" false "> <!--android:exported: Whether the service can be called or interacted with by other application components--
<intent-filter>
<category android:name= "Android.intent.category.DEFAULT"/>
<action android:name= "Com.w.learnintent.intent.action.myaty"/>
</intent-filter>
</activity>

3. In the Mainactivity.java file of the new project:

Findviewbyid (R.id.btnstartmyaty). Setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
try {
StartActivity (New Intent ("Com.w.learnintent.intent.action.myaty"));
}catch (Exception e) {
Toast.maketext (Mainactivity.this, "Unable to start the specified activity", Toast.length_short). Show (); Toast.length_short short time rendering
}
}

Concept and application of intent in Android (i)--show intent and implicit intent

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.