Intent可以说是Android的灵魂,程序跳转和传递数据的时候基本上就是靠Intent了。Intent在Android应用中是相当重要的,理解Intent对应用编程很有帮助。在Android的官方API文档里边对Intent是这样定义的:An Intent is an abstract description of an operation to be performed。一个Intent就是一次对将要执行的操作的抽象描述(真实够抽象的)。具体有一下3种形式:
1、通过startActivity方法来启动一个新的Activity。
2、通过Broadcast机制可以将一个Intent发送给任何对这个Intent感兴趣的BroadcastReceiver。
3、通过startService(Intent)或bindService(Intent, ServiceConnection, int)来和后台的Service交互。

下面来看一下如何通过startActivity方法启动一个新的Activity。
Intent最常用的用途就是连接一个应用当中的各个Activity,如果我们把Activity比作积木的话,那么Intent就好像是胶水,把不同的积木粘起来,构成我们搭建的房子。在程序当中如果要启动一个Activity的话,通常我们会调用startActivity方法,并把Intent作为参数传进去,如下所示:
startActivity(intent);
这个Intent或者指定了一个Activity,或者里边只包含了选定Activity的信息,但是具体启动哪个Activity是由系统去决定的,Android系统负责挑选一个最满足匹配挑选条件的Activity。

实例:IntentDemo
运行效果:
Android之Intent和Activity_第1张图片

代码清单:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.rainsong.intentdemo"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk        android:minSdkVersion="11"        android:targetSdkVersion="19" />    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">        <activity android:name="MainActivity"                  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="OtherActivity">        </activity>    </application></manifest>
布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="跳转到另一个Activity"    /></LinearLayout>
布局文件:other.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    ><TextView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="OtherActivity"    /></LinearLayout>
Java源代码文件:MainActivity.java
package com.rainsong.intentdemo;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 MainActivity extends Activity{    OnClickListener listener1 = null;    Button button1;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                listener1 = new OnClickListener() {            public void onClick(View v) {                Intent  intent1= new Intent(MainActivity.this, OtherActivity.class);                startActivity(intent1);            }        };        button1 = (Button)findViewById(R.id.button1);        button1.setOnClickListener(listener1);    }}

Java源代码文件:OtherActivity.java

package com.rainsong.intentdemo;import android.app.Activity;import android.os.Bundle;public class OtherActivity extends Activity{    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.other);    }}


更多相关文章

  1. Android开发之文件下载
  2. 解决android中使用shape文件画虚线不显示
  3. AndroidManifest.xml文件剖析 (二)
  4. 在android中显示网络图片及查看页面源代码
  5. android中使用sqlite、复制assets下的数据库到SD卡、支持大于1M
  6. Android第十八课 VS源码文件导入Android工程 中文乱码
  7. Android 主题之安装的APK主题文件

随机推荐

  1. mysql innodb的监控(系统层,数据库层)
  2. 详解mysql基本操作语句命令
  3. MySql服务未知原因消失解决方法
  4. CentOS下RPM方式安装MySQL5.6教程
  5. DROP TABLE在不同数据库中的写法整理
  6. MySQL thread_stack连接线程的优化
  7. MYSQL 解锁与锁表介绍
  8. Navicat中导入mysql大数据时出错解决方法
  9. mysql 导出CSV文件 并带表头的方法
  10. mysql导出查询结果到csv的实现方法