Intent的属性及Intent-filter配置——指定Action、Category调用系统Activity
Intent代表了启动某个程序组件的“意图”,实际上Intent对象不仅可以启动本应用内程序组件,也可启动Android系统的其他应用的程序组件,包括系统自带的程序组件——只要权限允许。
实际上Android内部提供了大量标准Action、Category常量,其中用于启动Activity的标准Action常量及对应的字符串如表5.2所示。
| Action常量 | 对应字符串 | 简单说明 |
|---|---|---|
| ACTION_MAIN | android.intent.action.MAIN | 应用程序入口 |
| ACTION_VIEW | android.intent.action.VIEW | 显示指定数据 |
| ACTION_ATTACH_DATA | android.intent.action.ATTACH_DATA | 指定某块数据将被附加到其他地方 |
| ACTION_EDIT | android.intent.action.EDIT | 编辑指定数据 |
| ACTION_PICK | android.intent.action.PICK | 从列表中选择某项并返回所选的数据 |
| ACTION_CHOOSER | android.intent.action.CHOOSER | 显示一个Activity选择器 |
| ACTION_GET_CONTENT | android.intent.action.GET_CONTENT | 让用户选择数据,并返回所选数据 |
| ACTION_DIAL | android.intent.action.DIAL | 显示拨号面板 |
| ACTION_CALL | android.intent.action.CALL | 直接向指定用户打电话 |
| ACTION_SEND | android.intent.action.SEND | 向其他人发送数据 |
| ACTION_SENDTO | android.intent.action.SENDTO | 向其他人发送消息 |
| ACTION_ANSWER | android.intent.action.ANSWER | 应答电话 |
| ACTION_INSERT | android.intent.action.INSERT | 插入数据 |
| ACTION_DELETE | android.intent.action.DELETE | 删除数据 |
| ACTION_RUN | android.intent.action.RUN | 运行数据 |
| ACTION_SYNC | android.intent.action.SYNC | 执行数据同步 |
| ACTION_PICK_ACTIVITY | android.intent.action.PICK_ACTIVITY | 用于选择Activity |
| ACTION_SEARCH | android.intent.action.SEARCH | 执行搜索 |
| ACTION_WEB_SEARCH | android.intent.action.WEB_SEARCH | 执行Web搜索 |
| ACTION_BATTERY_LOW | android.intent.action.ACTION_BATTERY_LOW | 电量低 |
| ACTION_MEDIA_BUTTON | android.intent.action.ACTION_MEDIA_BUTTON | 按下媒体按钮 |
| ACTION_PACKAGE_ADDED | android.intent.action.ACTION_PACKAGE_ADDED | 添加包 |
| ACTION_PACKAGE_REMOVED | android.intent.action.ACTION_PACKAGE_REMOVED | 删除包 |
| ACTION_FACTORY_TEST | android.intent.action.FACTORY_TEST | 工厂测试的入口点 |
| ACTION_BOOT_COMPLETED | android.intent.action.BOOT_COMPLETED | 系统启动完成 |
| ACTION_TIME_CHANGED | android.intent.action.ACTION_TIME_CHANGED | 时间改变 |
| ACITON_DATE_CHANGED | android.intent.action.ACTION_DATE_CHANGED | 日期改变 |
| ACTION_TIMEZONE_CHANGED | android.intent.action.ACTION_TIMEZONE_CHANGED | 时区改变 |
| ACTION_MEDIA_EJECT | android.intent.action.MEDIA_EJECT | 插入或拔出外部媒体 |
标准Category常量及对应的字符串如表5.3所示
| Category常量 | 对应字符串 | 简单说明 |
|---|---|---|
| CATEGORY_DEFAULT | android.intent.category.DEFAULT | 默认的Category |
| CATEGORY_BROWSABLE | android.intent.category.BROWSABLE | 指定该Activity能被浏览器安全调用 |
| CATEGORY_TAB | android.intent.category.TAB | 指定该Activity作为TabActivity的Tab页 |
| CATEGORY_LAUNCHER | android.intent.category.LAUNCHER | Activity显示顶级程序列表中 |
| CATEGORY_INFO | android.intent.category.INFO | 用于提供包信息 |
| CATEGORY_HOME | android.intent.category.HOME | 设置该Activity随系统启动而运行 |
| CATEGORY_PREFERENCE | android.intent.category.PREFERENCE | 该Activity是参数面板 |
| CATEGORY_TEST | android.intent.category.TEST | 该Activity是一个测试 |
| CATEGORY_CAR_DOCK | android.intent.category.CAR_DOCK | 指定手机被插入汽车底座(硬件)时运行该Activity |
| CATEGORY_DESK_DOCK | android.intent.category.DESK_DOCK | 指定手机被插入桌面底座(硬件)时运行该Activity |
| CATEGORY_CAR_MODE | android.intent.category.CAR_MODE | 设置该Activity可在车载环境下使用 |
表5.2、表5.3所列出的都只是部分较为常用的Action常量、Category常量、关于Intent所提供的全部Action常量、Category常量,应参考Android API文档中关于Intent的说明。
下面将以两个实例来介绍Intent系统Action、系统Category的用法。
实例:查看并获取联系人电话
这个程序将会在程序中提供一个按钮,用户单击该按钮时会显示系统的联系人列表,当用户单击指定联系人之后,程序将会显示该联系人的名字、电话。
该程序的界面布局代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<!-- 显示 联系人姓名的文本框 -->
<EditText android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false"/>
<!-- 显示联系人的电话的文本框 -->
<EditText android:id="@+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false"/>
<Button android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看联系人"/> </LinearLayout>
上面的界面布局中包含了两个文本框,一个按钮,其中按钮用于浏览系统联系人列表并选择其中的联系人。两个文本框分别用于显示联系人的名字、电话号码。
该程序的Java代码如下。
package com.example.studyintent; import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class SysAction extends Activity {
final int PICK_CONTACT=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sys_action);
Button bn=(Button)findViewById(R.id.bn);
//为bn按钮绑定事件监听器
bn.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建Intent
Intent intent=new Intent();
//设置Intent的Action属性
intent.setAction(Intent.ACTION_GET_CONTENT);
//设置Intent的Type属性
intent.setType("vnd.android.cursor.item/phone");
//启动Activity,并希望获取该Activity的结果
startActivityForResult(intent,PICK_CONTACT);
}});
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case (PICK_CONTACT):
if(resultCode==Activity.RESULT_OK)
{
//获取返回的数据
Uri contactData=data.getData();
CursorLoader cursorLoader=new CursorLoader(this,contactData,null,null,null,null);
//查询联系人信息
Cursor cursor=cursorLoader.loadInBackground();
//如果查询到指定的联系人
if(cursor.moveToFirst())
{
String contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//获取联系人的名字
String name=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String phoneNumber="此联系人暂未输入电话号码";
//根据联系人查询该联系人的详细信息
Cursor phones=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone._ID+"="+contactId, null, null); if(phones.moveToFirst())
{
//取出电话号码
phoneNumber=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); }
//关闭游标
phones.close();
EditText show=(EditText)findViewById(R.id.show);
//显示联系人的名称
show.setText(name);
EditText phone=(EditText)findViewById(R.id.phone);
//显示联系人的电话号码
phone.setText(phoneNumber);
}
//关闭游标
cursor.close(); }
break; }
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sys_action, menu);
return true;
} }
运行上面的程序,单击程序界面中“查看联系人”按钮,程序将会显示如图5.4所示的界面。

在图5.4所示的联系人列表中单击某个联系人,系统将会自动返回上一个Activity,程序会在上一个Activity中显示所选联系人的名字和电话,如图5.5所示。

上面的Intent对象除了设置Action属性之外,还设置了Type属性。
需要指出的是,由上面的程序需要查看系统联系人信息,因此不要忘了向该应用的Android Manifest.xml文件中增加相应的权限,也就是在AndroidManifest.xml文件中增加如下配置。
<uses-permission android:name="android.permission.READ_CONTACTS" />
实例:返回系统Home桌面
接下来的示例将会提供,当用户单击该按钮时,系统将会返回Home界面。这也需要Intent来实现,程序为Intent设置合适的Action、合适的Category属性,并根据该Intent来启动Activity即可返回Home界面。该示例程序如下。
package com.example.studyintent; import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class ReturnHome extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_return_home);
Button bn=(Button)findViewById(R.id.bn);
bn.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建Intent对象
Intent intent=new Intent();
//为Intent设置Action、Category属性
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
} });
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.return_home, menu);
return true;
} }
上面的程序中粗体字代码设置了Intent的Action为android.intent.action.MAIN字符串、Category属性为android.intent.category.HOME字符串,满足该Intent的Activity其实就是Android系统的Home界面。因此运行上面程序时“单击”按钮即可返回Home界面。
Intent的属性及Intent-filter配置——指定Action、Category调用系统Activity的更多相关文章
- 指定Action、Category调用系统Activity
1.Intent对象详解 Android的应用程序包含三种重要组件:Activity.Service.BroadcastReceiver,应用程序采用一致的方式来启动它们----都是依靠Intent来 ...
- Struts2中配置默认Action
1.当访问的Action不存在时,页面会显示错误信息,可以通过配置默认Action处理用户异常的操作:2.配置方法: 在struts.xml文件中的<package>下添加如下内容: ...
- Intent的属性及Intent-filter配置——Data、Type属性与intent-filter配置
Data属性通常用于向Action属性提供操作的数据,Data属性接受一个Uri对象,一个Uri对象通常通过如下形式的字符串来表示: content://com.android.contacts/co ...
- Intent的属性及Intent-filter配置——Action、Category属性与intent-filter属性
Intent的Action.Category属性都是一个普通的字符串,其中Action代表该Intent所要完成的一个抽象“动作”,而Category则用于为Action增加额外的附加列别的信息.通常 ...
- Intent的属性及Intent-filter配置——Component属性
Intent的Component属性需要接受一个ComponentName对象,ComponentName对象包含如下几个构造器. ComponentName(String pkg,String cl ...
- Intent的属性及Intent-filter配置——实例Action、Data属性启动系统Activity
一旦为Intent同时指定了Action.Data属性,那么Android将可根据指定的数据类型来启动特定的应用程序,并对指定数据类型执行相应的操作. 下面是几个Action属性.Data属性的组合. ...
- Intent的属性及Intent-filter配置——Extra属性——Flag属性
Intent的Extra属性通常用于在多个Action之间进行数据交换,Intent的Extra属性值应该是一个Bundle对象,Bundle对象的就像一个Map对象,它可以存入多组key-value ...
- Intent七大属性
一.Intent的作用是什么? 1.Intent 用于封装程序的”调用意图“.两个Activity之间,可以把需要交换的数据封装成Bundle对象,然后使用Intent携带Bundle对象,实现 ...
- Intent七大属性之总结
参考<疯狂android讲义>第5章 1.Intent 用于封装程序的"调用意图",不管想启动一个Acitivity.Service还是BroadcastReceive ...
随机推荐
- 兼容性,float
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- PAT (Advanced Level) 1043. Is It a Binary Search Tree (25)
简单题.构造出二叉搜索树,然后check一下. #include<stdio.h> #include<algorithm> using namespace std; +; st ...
- ubuntu 15.04怎么安装QQ
ubuntu 15.04怎么安装QQ | 浏览:468 | 更新:2015-07-21 10:20 1 2 3 4 5 6 7 分步阅读 新装的ubuntu不能没有QQ,我们需要安装QQ来进行及时交流 ...
- django 中文乱码问题
在使用JS 发送ajax到django后台的时候,可能会出现中文乱码问题 解决方案: 所有的HTMl 和py文件都使用utf-8编码,在创建数据库的时候指定使用utf8 :create databas ...
- javascript 回调, 单线程执行
原文: http://www.cnblogs.com/aaronjs/p/3322466.html 这里的"回调"并不是"阻塞",而会空出执行线程,直至操作完成 ...
- COC建筑拖动的实现
最近在玩COC,多体验一下手游的体验,因为自己毕竟一直是做页游的,有些观念需要转变一下. 好像偏了,玩了几次之后突然想起COC那个地图拖动的自己之前实现过,那是2010年左右的时候,模拟经营类页游大行 ...
- PAT (Advanced Level) 1084. Broken Keyboard (20)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- Extjs4 up 和down的用法
Extjs4.x中,每个组件都新增加了两个方法up()和down()方法.这两个方法都是用来获取组件的,下面我们来看下up()方法和down()方法的官方解释. Extjs4.x中,新增加了两个方法u ...
- Classification of text documents: using a MLComp dataset
注:原文代码链接http://scikit-learn.org/stable/auto_examples/text/mlcomp_sparse_document_classification.html ...
- (中等) HDU 5293 Tree chain problem,树链剖分+树形DP。
Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There are ...