Intent代表了启动某个程序组件的“意图”,实际上Intent对象不仅可以启动本应用内程序组件,也可启动Android系统的其他应用的程序组件,包括系统自带的程序组件——只要权限允许。

实际上Android内部提供了大量标准Action、Category常量,其中用于启动Activity的标准Action常量及对应的字符串如表5.2所示。

表5.2  启动Activity的标准Action
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所示

表5.3  标准Category
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的更多相关文章

  1. 指定Action、Category调用系统Activity

    1.Intent对象详解 Android的应用程序包含三种重要组件:Activity.Service.BroadcastReceiver,应用程序采用一致的方式来启动它们----都是依靠Intent来 ...

  2. Struts2中配置默认Action

    1.当访问的Action不存在时,页面会显示错误信息,可以通过配置默认Action处理用户异常的操作:2.配置方法:    在struts.xml文件中的<package>下添加如下内容: ...

  3. Intent的属性及Intent-filter配置——Data、Type属性与intent-filter配置

    Data属性通常用于向Action属性提供操作的数据,Data属性接受一个Uri对象,一个Uri对象通常通过如下形式的字符串来表示: content://com.android.contacts/co ...

  4. Intent的属性及Intent-filter配置——Action、Category属性与intent-filter属性

    Intent的Action.Category属性都是一个普通的字符串,其中Action代表该Intent所要完成的一个抽象“动作”,而Category则用于为Action增加额外的附加列别的信息.通常 ...

  5. Intent的属性及Intent-filter配置——Component属性

    Intent的Component属性需要接受一个ComponentName对象,ComponentName对象包含如下几个构造器. ComponentName(String pkg,String cl ...

  6. Intent的属性及Intent-filter配置——实例Action、Data属性启动系统Activity

    一旦为Intent同时指定了Action.Data属性,那么Android将可根据指定的数据类型来启动特定的应用程序,并对指定数据类型执行相应的操作. 下面是几个Action属性.Data属性的组合. ...

  7. Intent的属性及Intent-filter配置——Extra属性——Flag属性

    Intent的Extra属性通常用于在多个Action之间进行数据交换,Intent的Extra属性值应该是一个Bundle对象,Bundle对象的就像一个Map对象,它可以存入多组key-value ...

  8. Intent七大属性

    一.Intent的作用是什么?    1.Intent 用于封装程序的”调用意图“.两个Activity之间,可以把需要交换的数据封装成Bundle对象,然后使用Intent携带Bundle对象,实现 ...

  9. Intent七大属性之总结

    参考<疯狂android讲义>第5章 1.Intent 用于封装程序的"调用意图",不管想启动一个Acitivity.Service还是BroadcastReceive ...

随机推荐

  1. CodeForces 591B Rebranding

    水题 #include<cstdio> #include<cstring> #include<cmath> #include<vector> #incl ...

  2. mark一篇文章--用nodejs搭建一个本地反向代理环境

    调试线上代码的时候,我们经常遇到的一个问题就是 本地一套环境,线上一套环境,本地没有的文件用线上的这种需求.我简单来说下使用nodejs如何做到. 先说下不用nodejs我们怎么做,工具比如fiddl ...

  3. Delph组件如何使用自己的图标(转)

    源:http://blog.csdn.net/henreash/article/details/7298451

  4. Java虚拟机中的内存分配

    java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途以及创建和销毁的时间. 栈:存放的是局部变量,包括:1.用来保存基本数据类型的值:2.保存类 ...

  5. extJS4.2.0 环境搭建教程(一)

    一.环境搭建

  6. iOS 为label添加删除线

    UILabel *testLabel = [[ UILabel alloc] initWithFrame:CGRectMake(, , , )]; testLabel.numberOfLines = ...

  7. HUST 1600 Lucky Numbers

    暴力打表. #include<cstdio> #include<cstring> #include<cmath> #include<string> #i ...

  8. Golang测试技术

    本篇文章内容来源于Golang核心开发组成员Andrew Gerrand在Google I/O 2014的一次主题分享“Testing Techniques”,即介绍使用Golang开发 时会使用到的 ...

  9. 用weka来做Logistic Regression

    1.首先下载安装weka http://www.cs.waikato.ac.nz/ml/weka/downloading.html 2.打开weka,选择第一项Explorer 3.准备数据集文件,在 ...

  10. ios上 更改 状态栏(UIStatusBar)的颜色,你值得一看、收藏

    IOS上 关于状态栏的相关设置(UIStatusBar) 知识普及 ios上状态栏 就是指的最上面的20像素高的部分 状态栏分前后两部分,要分清这两个概念,后面会用到: 前景部分:就是指的显示电池.时 ...