疯狂android讲义---intent-filter
Intent 封装Android应用程序需要启动某个组件的“意图”。此外,还是组件之间通信的重要媒介。
android应用程序三种重要组件:activity,service,broadcastrecevier,应用程序采用了一致的方式来启动它们---intent
使用Intent启动不同组件的方法:
Activity:
1, startActivity(Intent intent)
2, startActivityForResult(Intent intent, int requestCode)
Service:
ComponentName startService(Intent service)
boolean bindService(Intent service,ServiceConnection conn,int flags)
BroadcastReceiver:
1, sendBroadcast(Intent intent)
2, sendBroadcast(Intent intent,String receiverPermission)
3, sendOrderedBroadcast(Intent intent,String receiverPermmission,BroadcastReceiver resultReceiver,Handler scheduler,int initialCode,String initialData,Bundle initialExtras)
4,sendOrderedBroadcast(Intent intent,String receiverPermission)
5, sendStickyBroadcast(Intent intent)
6, sendStickyOrderedBroadcast(Intent intent,BroadcastReceiver resultReceiver,Handler scheduler, int initialCode,String initialData,Bundle initialExtras)
5.2 Intent的属性及intent-filter配置
android根据intent的属性值来启动相应组件。
intent对象大致包含Component ,Action , Category , Data ,Type ,Extra , Flag。
1, Component:用于明确指定需要启动的组件
Intent的Component属性需要接受一个ComponentName对象,ComponentName对象有如下构造器:
ComponentName(String pkg,String cls)
ComponentName (Context pkg,String cls)
ComponentName(Context pkg,Class<?> cls)
本质就一个,创建一个ComponentName需要指定包名和类名,以便唯一确定一个组件。
//创建一个ComponentName对象
ComponentName comp = new ComponentName(ComponentAttr.this
, SecondActivity.class);
Intent intent = new Intent();
//为Intent设置Component属性
intent.setComponent(comp);
startActivity(intent);
由上面可以看到当程序通过intent的Component属性启动组件时,被启动的组件几乎不用使用<intent-filter…/>元素进行配置。
//获取该Activity对应的Intent的Component属性
ComponentName comp = getIntent().getComponent();
//显示该ComponentName对象的包名、类名
show.setText("组件包名为:" + comp.getPackageName()
+ "\n组件类名为:" + comp.getClassName());
5.2.2 Action ,Category 属性与 intent-filter配置
Intent的Action,Category属性都是一个普通的字符串,Action代表该intent所要完成的一个抽象“动作”,而category则用于为action增加额外的附加类别信息。通常二者会结合使用
action要完成的只是一个抽象的动作,这个动作具体由哪个组件来完成,action这个字符串并不管。比如android提供的标准action:intent.ACTION_VIEW它只是一个抽象的查看动作,但具体查看什么,启动哪个activity来查看,intent. ACTION_VIEW并不知道—这取决于activity的<intent-filter…/>配置,只要某个activity的<intent-filter…/>配置中包含了该ACTION_VIEW,该activity就可能被启动。
//创建Intent对象
Intent intent = new Intent();
//为Intent设置Action属性(属性值就是一个普通字符串)
intent.setAction(ActionAttr.CRAZYIT_ACTION);
startActivity(intent);
//获取该Activity对应的Intent的Action属性
String action = getIntent().getAction();
//显示Action属性
show.setText("Action为:" + action);
<activity android:name=".SecondActivity"
android:label="@string/app_name">
<intent-filter>
<!-- 指定该Activity能响应Action为指定字符串的Intent -->
<action android:name="org.crazyit.intent.action.CRAZYIT_ACTION" />
<!-- 指定该Action能响应Action属性为helloWorld的Intent -->
<action android:name="helloWorld" />
<!-- 指定该Action能响应Category属性为指定字符串的Intent -->
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
表明activity能相应action属性值为其中任意一项字符串的Intent
一个intent对象只能包括一个action属性,,但一个intent对象可以包含多个category属性 程序创建intent的时候,category默认属性值为intent.CATEGORY_DEFAULT(常量值为:android.intent.category.DEFAULT)的组件。
因此上面虽未指定category属性,但该intent已有一个默认值,因此被启动的activity对应的配置元素<intent-filter…/>元素至少还包括一个默认的category属性值。
//创建Intent对象
Intent intent = new Intent();
//为Intent设置Action、Category属性
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
满足上述intent的activity其实就是android系统的home桌面。(manifest.xml没有配置什么东西)
5.2.4 Data,Type属性
Data属性通常用于向Action属性提供操作的数据。
data属性接受一个uri对象,形式如下:
cotent://com.android.contacts/contacts/1
tel:123
冒号前面指定了数据类型,冒号后面的数据部分。
Type属性则用于指定data属性所指定数据的类型或MIME类型,通常当intent不指定data属性时Type属性次啊会起作用,否则android系统将会根据data属性值来分析数据类型。
一旦为intent同时指定了action,data属性,那么android将可根据指定的数据类型来启动特定的应用程序,并对指定数据执行相应的操作。
下面是几个action属性,data属性的组合。
ACTION_VIEW content://com.android.contacts/contacts/1:显示标识为1的联系人的信息。
ACTION_EDIT content://com.android.contacts/contacts/1:编辑标识为1的联系人的信息。
ACTION_DIAL content://com.android.contacts/contacts/1:显示向标识为1的联系人拨号的界面
ACTION_VIEW tel:123:显示向指定号码123拨号的界面{存在不确定}。
ACTION_DIAL tel:123显示向
ACTION_VIEW content://contacts/people/:显示所有联系人列表的信息,
Intent intent = new Intent();
String data = "http://www.crazyit.org";
//根据指定字符串解析出Uri对象
Uri uri = Uri.parse(data);
//为Intent设置Action属性
intent.setAction(Intent.ACTION_VIEW);
//设置Data属性
intent.setData(uri);
startActivity(intent);
//创建Intent
Intent intent = new Intent();
//为Intent设置Action属性(动作为:编辑)
intent.setAction(Intent.ACTION_EDIT);
String data = "content://com.android.contacts/contacts/1";
//根据指定字符串解析出Uri对象
Uri uri = Uri.parse(data);
//设置Data属性
intent.setData(uri);
startActivity(intent);
//创建Intent
Intent intent = new Intent();
//为Intent设置Action属性(动作为:拨号)
intent.setAction(Intent.ACTION_DIAL);
String data = "tel:13800138000";
//根据指定字符串解析出Uri对象
Uri uri = Uri.parse(data);
//设置Data属性
intent.setData(uri);
startActivity(intent);
5.2.5 Extra属性
intent的extra属性通常用于多个action之间进行数据交换,intent的extra属性值应该是一个bundle对象,。
5.3 使用intent创建tab页面
TabHost tabHost = getTabHost();
//使用Intent添加第一个Tab页面
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("已接电话"
, getResources().getDrawable(R.drawable.icon))
.setContent(new Intent(this, BeCalledActivity.class)));
//使用Intent添加第二个Tab页面
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("呼出电话")
.setContent(new Intent(this, CalledActivity.class)));
//使用Intent添加第三个Tab页面
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("未接电话")
.setContent(new Intent(this, NoCallActivity.class)));
更多相关文章
- Android开发常用属性
- ScrollView常用属性汇总
- Android属性之build.prop生成过程分析
- textView 属性总结
- Android平台上面输入法遮挡问题-android:windowSoftInputMode属
- android 属性android:visibility及 view的setVisibility方法值的
- 界面开发中布局属性全面剖析