android笔记6——intent的使用
2014-06-05 15:36
399 查看
今天挑出一节专门来说一下使用intent和intentfilter进行通信。
场景:一个Activity启动另一个Activity。
前面已经讲了Fragment的切换,Fragment顾名思义是基于碎片切换的,假如我们要切换屏幕,或者是service组件等等,这就要用到Intent。
此外还想说明一下,Intent还具有很好的设计思想在里面的。它将各种“启动意图”封装成一个一致编程模型,利于高层次的解耦。
1、Intent属性
Component属性
先来看一段代码:
针对ComponentName这个类:(intent.setComponent(componentName);)
构造方法:
再来看Intent类里面的一段源码:
我想我不用多说了的,懂java的人都知道的。。。
Action、Category属性与intent-filter的配置
首先,先来看一下intent-filter配置:这个配置一般写在AndroidManifest.xml:
intent-filter里面包含了action和category,这两个标签与Intent里面action和category属性是一一对应的。
来看下一段代码:
这个作用和上面是一样的,通过这两种方式,我们都可以启动其他的activity。通常我们将Component这种方式称为“显式Intent”,顾名思义,第二种Action的方式称为“隐式Intent”,作用都一样的,一般开发中为了让程序更具可读性,采用显式的方式比较多。
此外,看一下下面的标准Action和Category表:
2、Intent为Activity之间传值
前面是已经说了启动切换的问题,那现在如果有从当前的activity携带值进行跳转到另一个Activity,这个应该怎么操作呢?
这里我们需要使用到Intent的Extra属性:
下面先看一段代码:
从上面可以看出,通过Bundle这个类,我们可以构造任意类型的参数,而且这种方式极力推荐的。
具体操作方法,请查阅其API。
那怎么样接收参数呢?
看下面代码:
从上面代码可以看到,还是使用Bundle这个类。
场景:一个Activity启动另一个Activity。
前面已经讲了Fragment的切换,Fragment顾名思义是基于碎片切换的,假如我们要切换屏幕,或者是service组件等等,这就要用到Intent。
此外还想说明一下,Intent还具有很好的设计思想在里面的。它将各种“启动意图”封装成一个一致编程模型,利于高层次的解耦。
1、Intent属性
Component属性
先来看一段代码:
<span style="white-space:pre"> </span>Intent intent = new Intent(); ComponentName componentName = new ComponentName(this, EventsActivity.class); intent.setComponent(componentName); startActivity(intent);这段代码的功能是用作从当前的activity启动到EventsActivity。
针对ComponentName这个类:(intent.setComponent(componentName);)
构造方法:
public ComponentName(String pkg, String cls) public ComponentName(Context pkg, String cls) public ComponentName(Context pkg, Class<?> cls)<span style="white-space:pre"> </span>//上面代码中使用到的构造,一般也是常用方法
再来看Intent类里面的一段源码:
public Intent setClass(Context packageContext, Class<?> cls) { mComponent = new ComponentName(packageContext, cls); return this; } public Intent setClassName(String packageName, String className) { mComponent = new ComponentName(packageName, className); return this; } public Intent setClassName(Context packageContext, String className) { mComponent = new ComponentName(packageContext, className); return this; }
我想我不用多说了的,懂java的人都知道的。。。
Action、Category属性与intent-filter的配置
首先,先来看一下intent-filter配置:这个配置一般写在AndroidManifest.xml:
<activity android:name="com.xmind.activity.TestActivity" android:label="@string/title_activity_test" > <intent-filter > <span style="white-space:pre"> </span><action android:name="com.xmind.intent.action.TEST_ACTION" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
intent-filter里面包含了action和category,这两个标签与Intent里面action和category属性是一一对应的。
来看下一段代码:
Intent intent = new Intent(); intent.setAction("com.xmind.intent.action.TEST_ACTION");
intent.addCategory("android.intent.category.DEFAULT"); startActivity(intent);
这个作用和上面是一样的,通过这两种方式,我们都可以启动其他的activity。通常我们将Component这种方式称为“显式Intent”,顾名思义,第二种Action的方式称为“隐式Intent”,作用都一样的,一般开发中为了让程序更具可读性,采用显式的方式比较多。
此外,看一下下面的标准Action和Category表:
2、Intent为Activity之间传值
前面是已经说了启动切换的问题,那现在如果有从当前的activity携带值进行跳转到另一个Activity,这个应该怎么操作呢?
这里我们需要使用到Intent的Extra属性:
下面先看一段代码:
<span style="white-space:pre"> </span>Intent intent = new Intent(); intent.setAction("com.xmind.intent.action.TEST_ACTION"); intent.putExtra("test1", 1); Bundle bundle = new Bundle(); bundle.putBoolean("test2", false); bundle.putSerializable("test3", new Person("Mr.稻帅",25)); intent.putExtras(bundle); startActivity(intent);
从上面可以看出,通过Bundle这个类,我们可以构造任意类型的参数,而且这种方式极力推荐的。
具体操作方法,请查阅其API。
那怎么样接收参数呢?
看下面代码:
<span style="white-space:pre"> </span>Intent intent = getIntent(); Bundle bundle = intent.getExtras(); Person person = (Person) bundle.getSerializable("test3"); textView = (TextView) findViewById(R.id.person_name); textView.setText(person.getName()); textView = (TextView) findViewById(R.id.person_age); textView.setText(person.getAge()+""); System.out.println(bundle.getInt("test1")); System.out.println(bundle.getBoolean("test2")); System.out.println(bundle.getSerializable("test3"));
从上面代码可以看到,还是使用Bundle这个类。
相关文章推荐
- Android笔记之intent使用(一)
- Android笔记----使用Intent和Android应用资源
- Android笔记之PenddingIntent使用
- Android 笔记 -- Activity intent经常使用的 FLAG 集合
- 笔记40 | Android通讯之Intent的简单使用
- Android笔记:intent方法使用总结
- android学习序列笔记之Intent使用方法
- Android笔记四.Intent对象的使用及.apk反编译
- Android(java)学习笔记146:Bundle和Intent类使用和交互
- Android笔记:Android 使用Intent拨打电话的两种方式以及差别
- android笔记之activity使用intent通信
- Android笔记:Intent的Component使用
- android笔记6——intent的使用
- 我的Android笔记(三)——使用Intent在Activity间传值
- Android 开发笔记—— Intent的使用
- Android使用Intent Filter来响应隐式Intent
- Android(OPhone) 学习笔记3 - Mail API 的使用
- Android(OPhone) 学习笔记2 - Home API 的使用
- Android 系统应用调用,intent的使用总结
- Android Intent使用