当前位置:操作系统 > 安卓/Android >>

Activity详解 Intent显式跳转和隐式跳转

Activity 生命周期
 
 
 
 
 
 
显式 Intent 调用
 
 
 
 
 1     //创建一个显式的 Intent 对象(方法一:在构造函数中指定)
 2      Intent intent = new Intent(Intent_Demo1.this, Intent_Demo1_Result1.class);
 3      
 4      Bundle bundle = new Bundle();
 5      bundle.putString("id", strID);
 6      intent.putExtras(bundle);
 7      
 8      intent.putExtra("name", "bbb");
 9      intent.putExtra("userInfo", new UserInfo(1, "name"));
10      startActivity(intent);
11      
12      //创建一个显式的 Intent 对象(方法二:用 setClass 方法)
13      Intent intent = new Intent();
14      Bundle bundle = new Bundle();
15      bundle.putString("id", strID);
16      intent.setClass(Intent_Demo1.this, Intent_Demo1_Result1.class);
17      intent.putExtras(bundle);
18      startActivity(intent);
19      
20      //创建一个显式的 Intent 对象(方法三:用 setClass 方法)
21      Intent intent = new Intent();
22      Bundle bundle = new Bundle();
23      bundle.putString("id", strID);
24      intent.setClassName(Intent_Demo1.this, "com.great.activity_intent.Intent_Demo1_Result1");
25      intent.putExtras(bundle);
26      startActivity(intent);
27      
28      //创建一个显式的 Intent 对象(方法四:用 setComponent 方法)
29      Intent intent = new Intent();
30      Bundle bundle = new Bundle();
31      bundle.putString("id", strID);
32      //setComponent方法的参数:ComponentName
33      intent.setComponent(new ComponentName(Intent_Demo1.this, Intent_Demo1_Result1.class));
34      intent.putExtras(bundle);
35      startActivity(intent);
 
 
 
Intent隐式跳转 Action
 
 
 1     //创建一个隐式的 Intent 对象:Action 动作
 2     /**
 3      * 这里指定的是 AndroidManifest.xml 文件中配置的
 4      * <intent-filter>标签中的<action android:name="com.great.activity_intent.Intent_Demo1_Result3" />
 5      * 所在的 Activity,注意这里都要设置 <category android:name="android.intent.category.DEFAULT" />
 6      */
 7     Intent intent = new Intent();
 8     //设置 Intent 的动作
 9     intent.setAction("com.great.activity_intent.Intent_Demo1_Result3");
10     Bundle bundle = new Bundle();
11     bundle.putString("id", strID);
12     intent.putExtras(bundle);
13     startActivity(intent);
 
AndroidManifest.xml
 
 
1    <activity android:name="Intent_Demo1_Result3"
2                   android:label="Intent_Demo1_Result3">
3             <intent-filter>
4                 <action android:name="com.great.activity_intent.Intent_Demo1_Result3" />
5                 <category android:name="android.intent.category.DEFAULT" />
6             </intent-filter>
7         </activity>
 
 
 
Category 类别
 
 
 1 //创建一个隐式的 Intent 对象:Category 类别
 2 Intent intent = new Intent();
 3 intent.setAction("com.great.activity_intent.Intent_Demo1_Result33");
 4 /**
 5  * 不指定 Category 或 只指定 AndroidManifest.xml 文件中 <intent-filter> 标签中配置的任意一个 Category
 6  * <category android:name="android.intent.category.DEFAULT" /> 除外,就可以访问该 Activity,
 7  */
 8 intent.addCategory(Intent.CATEGORY_INFO);
 9 intent.addCategory(Intent.CATEGORY_DEFAULT);
10 Bundle bundle = new Bundle();
11 bundle.putString("id", strID);
12 intent.putExtras(bundle);
13 startActivity(intent);
 
AndroidManifest.xml
 
 
  <activity android:name="Intent_Demo1_Result2"
                  android:label="Intent_Demo1_Result2">
            <intent-filter>
                
                <category android:name="android.intent.category.INFO" />
                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
                
            </intent-filter>
        </activity>
 
Date 数据 跳转
 
 
 1 //创建一个隐式的 Intent 对象,方法四:Date 数据
 2 Intent intent = new Intent();
 3 Uri uri = Uri.parse("http://www.great.org:8080/folder/subfolder/etc/abc.pdf");
 4 
 5 //注:setData、setDataAndType、setType 这三种方法只能单独使用,不可共用                
 6 //要么单独以 setData 方法设置 URI
 7 //intent.setData(uri);
 8 //要么单独以 setDataAndType 方法设置 URI 及 mime type
 9 intent.setDataAndType(uri, "text/plain");
10 //要么单独以 setDataAndType 方法设置 Type
11 //intent.setType("text/plain");
12 
13 /**
14  * 不指定 Category 或 只指定 AndroidManifest.xml 文件中 <intent-filter> 标签中配置的任意一个 Category
15  * <category android:name="android.intent.category.DEFAULT" /> 除外,就可以访问该 Activity
16  */
17 Bundle bundle = new Bundle();
18 bundle.putString("id",
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,