Activity 以及 Intent的使用

原文链接::http://www.360doc.com/content/10/0205/00/155970_15164829.shtml#



Activity 的 Intent Filter

Intent Filter 描述了一个组件愿意接收什么样的 Intent 对象,Android 将其抽象为 android.content.IntentFilter 类。在 Android 的 AndroidManifest.xml 配置文件中可以通过 <intent-filter > 节点为一个 Activity 指定其 Intent Filter,以便告诉系统该 Activity 可以响应什么类型的 Intent。

当程序员使用 startActivity(intent) 来启动另外一个 Activity 时,如果直接指定 intent 了对象的 Component 属性,那么 Activity Manager 将试图启动其 Component 属性指定的 Activity。否则 Android 将通过 Intent 的其它属性从安装在系统中的所有 Activity 中查找与之最匹配的一个启动,如果没有找到合适的 Activity,应用程序会得到一个系统抛出的异常。这个匹配的过程如下:


图 4. Activity 种 Intent Filter 的匹配过程


Action 匹配

Action 是一个用户定义的字符串,用于描述一个 Android 应用程序组件,一个 Intent Filter 可以包含多个 Action。在 AndroidManifest.xml 的 Activity 定义时可以在其 <intent-filter > 节点指定一个 Action 列表用于标示 Activity 所能接受的“动作”,例如:

  <intent-filter >
             <action android:name="android.intent.action.MAIN" />
             <action android:name="com.zy.myaction" />
             ……
             </intent-filter>
             

 

如果我们在启动一个 Activity 时使用这样的 Intent 对象:

  Intent intent =new Intent();
             intent.setAction("com.zy.myaction");
             

 

那么所有的 Action 列表中包含了“com.zy.myaction ”的 Activity 都将会匹配成功。

Android 预定义了一系列的 Action 分别表示特定的系统动作。这些 Action 通过常量的方式定义在 android.content. Intent 中,以“ACTION_ ”开头。我们可以在 Android 提供的文档中找到它们的详细说明。

URI 数据匹配

一个 Intent 可以通过 URI 携带外部数据给目标组件。在 <intent-filter > 节点中,通过 <data/> 节点匹配外部数据。

mimeType 属性指定携带外部数据的数据类型,scheme 指定协议,host、port、path 指定数据的位置、端口、和路径。如下:

  <data android:mimeType="mimeType" android:scheme="scheme"
             android:host="host" android:port="port" android:path="path"/>
             

 

如果在 Intent Filter 中指定了这些属性,那么只有所有的属性都匹配成功时 URI 数据匹配才会成功。

Category 类别匹配

<intent-filter > 节点中可以为组件定义一个 Category 类别列表,当 Intent 中包含这个列表的所有项目时 Category 类别匹配才会成功。



全屏的 Activity

要使一个 Activity 全屏运行,可以在其 onCreate() 方法中添加如下代码实现:

  // 设置全屏模式
             getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
             WindowManager.LayoutParams.FLAG_FULLSCREEN);
             // 去除标题栏
             requestWindowFeature(Window.FEATURE_NO_TITLE);
            
 

 

在 Activity 的 Title 中加入进度条

为了更友好的用户体验,在处理一些需要花费较长时间的任务时可以使用一个进度条来提示用户“不要着急,我们正在努力的完成你交给的任务”。如下图:

在 Activity 的标题栏中显示进度条不失为一个好办法,下面是实现代码:

  // 不明确进度条
             requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
             setContentView(R.layout.main);
             setProgressBarIndeterminateVisibility(true);
             // 明确进度条
             requestWindowFeature(Window.FEATURE_PROGRESS);
             setContentView(R.layout.main);
             setProgress(5000);
 

猜你喜欢

转载自tedyin.iteye.com/blog/1683518