Android development-Intent and Activity

Source: Internet
Author: User

Basic functions of Intent:

An Intent object contains a set of information:

1. Component name

2. Action

3. Date

4. Category

5. Extras

6. Flags

 

Intent Overview

• Intent is the core component of Android. It uses messages to implement interaction between applications. Such messages describe the actions, data, and additional data of an application, the system is responsible for finding the corresponding component through the description of the Intent, and passing the Intent to the called component to complete the call of the component.
• Intent consists of actions, Data, categories, types, components, and extended information. Each group is represented by corresponding attributes and provides methods for setting and obtaining corresponding attributes.

Cheng Attribute Setting attributes How to Get attributes
Action Action SetAction () GetAction ()
Data Data SetData () GetData ()
Category Category SetCategory ()  
Type Type SetType () GetType ()
Components Component SetComponent ()
SetClass ()
SetClassName ()
GetComponent ()
Extended information Extra PutExtra () GetXXXExtra () obtains data of different data types. For example, if the int type is used, getIntExtra () is used, and the string uses getStringExtra ()
GetExtras () Get Bundle package
 

 


 

In Android, Intent and Activity operate on each other directly. The most common purpose of Intent is to bind application components. Intent is used to start, stop, and transmit between activities of an application.

To open different interfaces (corresponding to an Activity) in the application, call startActivity and input an Intent, for example, startActivity (myIntent );

We can get a general idea of the relationship between Activity and Intent.

The following example shows how to use Intent to operate the Activity:

1. Create an Android Project (Activity02)

The code list is as follows:

1) Android02.java

2) otherActivity. java

3) main. xml

4) other. xml

5) string. xml

6) Android02Manifest. xml

 

= "Android02.java

[Java] <span style = "font-family: FangSong_GB2312; font-size: 18px;"> package com. Activity02;
 
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
 
Public class Activity02 extends Activity {
/** Called when the activity is first created .*/
Private Button myButton = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MyButton = (Button) findViewById (R. id. myButton );
MyButton. setText ("myButton ");
MyButton. setOnClickListener (new MyButtonListener ());
}


 
Class MyButtonListener implements OnClickListener {
 
Public void onClick (View v ){
// TODO Auto-generated method stub
// Generate an Intent object
Intent intent = new Intent ();
Intent. setClass (Activity02.this, otherActivity. class );
Activity02.this. startActivity (intent );
}

}

} </Span>
<Span style = "font-family: FangSong_GB2312; font-size: 18px;"> package com. Activity02;

Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;

Public class Activity02 extends Activity {
/** Called when the activity is first created .*/
Private Button myButton = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MyButton = (Button) findViewById (R. id. myButton );
MyButton. setText ("myButton ");
MyButton. setOnClickListener (new MyButtonListener ());
}

 

Class MyButtonListener implements OnClickListener {

Public void onClick (View v ){
// TODO Auto-generated method stub
// Generate an Intent object
Intent intent = new Intent ();
Intent. setClass (Activity02.this, otherActivity. class );
Activity02.this. startActivity (intent );
}

}
 
} </Span>

 

= "OtherActivity. java

[Java]
<Span style = "font-family: FangSong_GB2312; font-size: 18px;"> package com. Activity02;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;
 
 
Public class otherActivity extends Activity {
Private TextView myTextView = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. other );
MyTextView = (TextView) findViewById (R. id. myTextView );
MyTextView. setText (R. string. other );
}

 
}
</Span>
<Span style = "font-family: FangSong_GB2312; font-size: 18px;"> package com. Activity02;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;


Public class otherActivity extends Activity {
Private TextView myTextView = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
SetContentView (R. layout. other );
MyTextView = (TextView) findViewById (R. id. myTextView );
MyTextView. setText (R. string. other );
}
 

}
</Span>
 

= "Main. xml

[Html]
<Span style = "font-family: FangSong_GB2312; font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
 
<Button
Android: id = "@ + id/myButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
 
</LinearLayout> </span>
<Span style = "font-family: FangSong_GB2312; font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">

<Button
Android: id = "@ + id/myButton"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>

</LinearLayout> </span>

 

= "Other. xml

[Html]
<Span style = "font-family: FangSong_GB2312; font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
 
<TextView
Android: id = "@ + id/myTextView"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</LinearLayout>
</Span>
<Span style = "font-family: FangSong_GB2312; font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">

<TextView
Android: id = "@ + id/myTextView"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>
</LinearLayout>
</Span>

 

= "String. xml

 

[Html]
<Span style = "font-family: FangSong_GB2312; font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
 
<String name = "hello"> Hello World, Activity02! </String>
<String name = "app_name"> Activity02 </string>
<String name = "other"> otherActivity </string>
 
</Resources> </span>
<Span style = "font-family: FangSong_GB2312; font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>

<String name = "hello"> Hello World, Activity02! </String>
<String name = "app_name"> Activity02 </string>
<String name = "other"> otherActivity </string>

</Resources> </span>

 

= "Android02Manifest. xml

[Html]
<Span style = "font-family: FangSong_GB2312; font-size: 18px;"> <? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. Activity02"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
 
<Uses-sdk android: minSdkVersion = "4"/>
 
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Activity
Android: name = ". Activity02"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
 
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Activity android: name = ". otherActivity" android: label = "@ string/other"/>
</Application>
 
</Manifest> </span>


Simulator running result:


Press the Button to execute otherActivity.







From wwj_748

 


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.