zoukankan      html  css  js  c++  java
  • Android中目的地Intent的使用

    一、什么是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里面配置声明。一下为本文用到的程序配置文件

     

      

    <?xml version="1.0" encoding="utf-8"?>
    
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    
    package="com.halzhang.android.intent" android:versionCode="1"
    
         android:versionName="1.0">
    
         <application android:icon="@drawable/icon" android:label="@string/app_name">
    
             <activity android:name=".IntentDemo" 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=".AnotherActivity" android:label="another">
    
                 <intent-filter>
    
                     <action android:name="android.intent.action.EDIT" />
    
                     <!-- category一定要配置,否则报错:找不到Activity -->
    
                     <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
    
             </activity>
    
     
    
             <activity android:name=".AnotherActivity2" android:label="another2">
    
                 <intent-filter>
    
                    <action android:name="android.intent.action.EDIT" />
    
                     <category android:name="android.intent.category.DEFAULT" />
    
                 </intent-filter>
    
             </activity>
    
         </application>
    
         <uses-sdk android:minSdkVersion="3" />
    
         <!-- 
    上面配置的两个activity具有相同的action类型,都为“android.intent.action.EDIT” 当Intent的action属性为Intent.ACTION_EDIT时,系统不知道转向哪个Activity时, 就会弹出一个Dialog列出所有action为“android.intent.action.EDIT”的 Activity供用户选择 -->
    </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中执行如下代码:

     

    Bundle bundle = new Bundle();
    
    bundle.putStringArray("NAMEARR", nameArr);
    
    Intent intent = new Intent(Main.this, CountList.class);
    
    intent.putExtras(bundle);
    
    startActivity(intent);

    在CountList中,代码如下:

     

       1:  Bundle bundle = this.getIntent().getExtras(); 

       2:  String[] arrName = bundle.getStringArray("NAMEARR"); 

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

  • 相关阅读:
    Base4.net和IronPython的一些相关东东
    WPF E 文章汇总
    Novell 发布Mono 1.2 推动.NET跨平台
    Google真好,这么多的工具……
    bootstrap源码学习与示例:bootstrappopover
    bootstrap源码学习与示例:bootstrapscrollspy
    bootstrap源码学习与示例:bootstrapcollapse
    bootstrap源码学习与示例:bootstrapaffix
    bootstrap源码学习与示例:bootstraptab
    mass Framework attr模块 v3
  • 原文地址:https://www.cnblogs.com/chen-lhx/p/4643929.html
Copyright © 2011-2022 走看看