Android intent implicit intent and display intent (activity jump)

Source: Internet
Author: User

There are two types of intentions in android: explicit and implicit,

Explicitly requires that you know the package and class of the activated component

For implicit intent, you only need to know the actions and data of the jump activity to activate the corresponding component.

A main activity B receives the redirected activity

Procedure

1: declare B in the main configuration file at least one android: name attribute.

[Html]
<Activity
Android: name = ". DemoActivity"
Android: label = "@ string/demoActivity">
<Intent-filter>
 
<! -- The intent name specified in the hermit intent can be defined to match multiple items. -->
<Action android: name = "com. itcast. intent. DemoActivity"/>
<! -- The type of the activity to be specified for the implicit intent. You can define this value by yourself. The DEFAULT value android. intent. category. DEFAULT is not required during the call. Multiple types can be matched. -->
<Category android: name = "android. intent. category. DEFAULT"/>
<! -- Specify the type and host of the activity value. If this item is specified, the data and host name must be input when the activity is redirected. -->
<Data
Android: host = "cn. itcast. demo"
Android: scheme = "itcast"/>
</Intent-filter>
</Activity>
 

2: select A textview and button in the layout file of A and add A click event.

[Html]
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "first activity"/>
 
<Button
Android: id = "@ + id/button"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: onClick = "open"
Android: text = "jump"/>
3: Set intent triggered by click event

[Java]
/**
* Intention: Describe an action and activate a component. To use other activities, you need to configure attributes such as the activity name in the main file.
* Intent is going to do one thing
*
* @ Param view
*/
Public void open (View view ){
/**
* Method 1
*/
Intent intent1 = new Intent ();
// 1. It is the current package name. 2. The class name of the jump activity. You must add the package name.
Intent1.setClassName ("com. itcast. intent", "com. itcast. intent. DemoActivity ");
// StartActivity (intent1 );
/**
* Method 2
*/
Intent intent2 = new Intent (this, DemoActivity. class );
// StartActivity (intent2 );
/**
* Method 3
*/
Intent intent3 = new Intent ();
ComponentName component = new ComponentName ("com. itcast. intent", "com. itcast. intent. DemoActivity ");
Intent3.setComponent (component );
// StartActivity (intent3 );
/**
* The preceding three methods require that the packages and classes of the activated components be known as the explicit
*/
//************************************** ****************************//
/**
* For implicit intent, you only need to know the actions and data of the jump activity to activate the corresponding component. <br>
* To activate components of another program
*/
Intent intent = new Intent ();
Intent. setAction ("com. itcast. intent. DemoActivity ");
// This item must be written no matter whether the main configuration file contains the category that declares the jump activity. Otherwise, an error is returned for the activity. Other types of activity may be recognized by the system.
Intent. addCategory ("android. intent. category. DEFAULT ");
// If the data attribute is specified when the activity is declared, you must set the data attribute value during the jump, which is equal to the configured attribute value. Otherwise, an error cannot be found.
Intent. setData (Uri. parse ("itcast: // cn. itcast. demo "));
// StartActivity (intent );
Intent imageIntent = new Intent ();
ImageIntent. setAction (Intent. ACTION_PICK );
ImageIntent. setType ("image/*"); // you can specify the data type.
StartActivity (imageIntent );
}

DemoActivity does not need to do anything. It only needs to inherit the activity and override its oncreate method. This example is just a few methods to test the activity jump.

Related Article

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.