intent

一、什么是Intent? Intent的中文意思是目的。在Android中也是目的的意思。就是我们要去哪里,从这个activity要前往另一个Activity就需要用到 Intent。 示例代码一: 1: //定义一个Intent 2: Intent intent = new Intent(IntentDemo. this , AnotherActivity2. class );

http://www.fengfly.com/ http://www.fengfly.com/html/JavaScript/ 专业技术教程网 www.fengfly.com ..http://www.fengfly.com/QQ...

一、什么是Intent?

Intent的中文意思是目的。在Android中也是“目的”的意思。就是我们要去哪里,从这个activity要前往另一个Activity就需要用到Intent。

示例代码一:

   1:
 //定义一个Intent
   2:
 Intent intent = new
 Intent(IntentDemo.this
, AnotherActivity2.class
);
   3:
 //启动Activity
   4:
 startActivity(intent);

以上示例代码的作用是从IntentDemo这个activity切换到AnotherActivity2。这是Intent其中一种构造方法,指 定两个Activity。为什么需要指定两个活动呢?因为在Android中有一个活动栈,这样的构造方式才能确保正确的将前一个活动压入栈中,才能在触 发返回键的时候活动能够正确出栈。

注意:所有的Activity都必须先在AndroidManifest.xml里面配置声明。一下为本文用到的程序配置文件

   1:
 <?
xml
 version
="1.0"
 encoding
="utf-8"
?>
   2:
 <
manifest
 xmlns:android
="http://schemas.android.com/apk/res/android"
   3:
     package
="com.halzhang.android.intent"
 android:versionCode
="1"
   4:
     android:versionName
="1.0"
>
   5:
     <
application
 android:icon
="@drawable/icon"
 android:label
="@string/app_name"
>
   6:
         <
activity
 android:name
=".IntentDemo"
 android:label
="@string/app_name"
>
   7:
             <
intent-filter
>
   8:
                 <
action
 android:name
="android.intent.action.MAIN"
 />
   9:
                 <
category
 android:name
="android.intent.category.LAUNCHER"
 />
  10:
             </
intent-filter
>
  11:
         </
activity
>
  12:
         <
activity
 android:name
=".AnotherActivity"
 android:label
="another"
>
  13:
             <
intent-filter
>
  14:
                 <
action
 android:name
="android.intent.action.EDIT"
 />
  15:
                 <!-- category一定要配置,否则报错:找不到Activity -->
  16:
                 <
category
 android:name
="android.intent.category.DEFAULT"
 />
  17:
             </
intent-filter
>
  18:
         </
activity
>
  19:
  
  20:
         <
activity
 android:name
=".AnotherActivity2"
 android:label
="another2"
>
  21:
             <
intent-filter
>
  22:
                 <
action
 android:name
="android.intent.action.EDIT"
 />
  23:
                 <
category
 android:name
="android.intent.category.DEFAULT"
 />
  24:
             </
intent-filter
>
  25:
         </
activity
>
  26:
     </
application
>
  27:
     <
uses-sdk
 android:minSdkVersion
="3"
 />
  28:
     <!-- 
  29:
         上面配置的两个activity具有相同的action类型,都为“android.intent.action.EDIT”
  30:
         当Intent的action属性为Intent.ACTION_EDIT时,系统不知道转向哪个Activity时,
  31:
         就会弹出一个Dialog列出所有action为“android.intent.action.EDIT”的
  32:
         Activity供用户选择
  33:
      -->
  34:
 </
manifest
>
 

二、Intent的构造函数

公共构造函数:

1、Intent() 空构造函数

2、Intent(Intent o) 拷贝构造函数

3、Intent(String action) 指定action类型的构造函数

4、Intent(String action, Uri uri) 指定Action类型和Uri的构造函数,URI主要是结合程序之间的数据共享ContentProvider

5、Intent(Context packageContext, Class<?> cls) 传入组件的构造函数,也就是上文提到的

6、Intent(String action, Uri uri, Context packageContext, Class<?> cls) 前两种结合体

Intent有六种构造函数,3、4、5是最常用的,并不是其他没用!

Intent(String action, Uri uri)  的action就是对应在AndroidMainfest.xml中的action节点的name属性值。在Intent类中定义了很多的Action和Category常量。

示例代码二:

   1:
 Intent intent = new Intent(Intent.ACTION_EDIT, null);
   2:
 startActivity(intent);

示例代码二是用了第四种构造函数,只是uri参数为null。执行此代码的时候,系统就会在程序主配置文件AndroidMainfest.xml中寻找

<action android:name="android.intent.action.EDIT" />对应的Activity,如果对应为多个activity具有<action android:name="android.intent.action.EDIT" />此时就会弹出一个dailog选择Activity,如下图:

 

 

如果是用示例代码一那种方式进行发送则不会有这种情况。

三、利用Intent在Activity之间传递数据

在Main中执行如下代码:

   1:
 Bundle bundle = new
 Bundle();
   2:
 bundle.putStringArray("NAMEARR"
, nameArr);
   3:
 Intent intent = new
 Intent(Main.this
, CountList.class
);
   4:
 intent.putExtras(bundle);
   5:
 startActivity(intent);

 

在CountList中,代码如下:

   1:
 Bundle bundle = this
.getIntent().getExtras();
   2:
 String[] arrName = bundle.getStringArray("NAMEARR"
);

以上代码就实现了Activity之间的数据传递!

转自:http://www.cnblogs.com/halzhang/archive/2010/05/28/1746592.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值