利用Intents实现软件的开放接口

今年九月份的统计数据显示,目前在Android Market中已经有超过10K的Softwares和Games提供给Android手机用户下载。一些曾经在IPhone APP Store创下辉煌战绩的Applications已经开始集体移植到这个拥有巨大潜力的平台。官方第二届Android Development Challenge(简称:ADC2)已经进入到了最后评审阶段,据称这次拥有大量惊奇创新的应用软件,AR会针对排名靠前的一些软件和游戏提供比较详细的技术剖析,希望大家保持关注。今天要说的内容与创新无关,主要是讲解如何将现有功能互补的软件通过Intents无 缝整合,使其最大限度的提高用户体验(不同的软件之间相互协调,不但可以达到优势互补的目的,而且通过相互间的合作可以在某个领域创造出相对完整的应用体 验)。

一些有关Intent异常的处理方法(no activity matches)

通常系统中的应用程序会借助于抛出Intent的方法,调用程序以外的资源来处理当前需求。这种方式可以提高系统资源利用率,而且当存在多个解决方法时,可以供用户作出最优化选择。然而,有一种特殊的情况却不容忽视,当前Intent无法在系统中获得任何可利用资源。这是一个必须要面对的客观情况,这里给出两种针对这种情况的解决方法。
第一种方法是提前预判,预判特定的Intent在当前系统的运行条件下是否可用,这样可以根据不同的系统配置提前屏蔽某些无法正常执行的功能或者引导用户通过某种方式提前为系统增加必要的运行条件,具体的可执行方法参照下面的代码:
/**
* Indicates whether the specified action can be used as an intent. This
* method queries the package manager for installed packages that can
* respond to an intent with the specified action. If no suitable package is
* found, this method returns false.
*
* @param context The application’s environment.
* @param action [...]

关于Activity和Task的设计思路和方法

Activity和Task是Android Application Framework架构中最基础的应用,开发者必须清楚它们的用法和一些开发技巧。本文用大量的篇幅并通过引用实例的方式一步步深入全面讲解它们的基础原理(underlying principles)和架构(mechanisms),例如:Navigation、Multitasking、activity re-use、intents和activity stack等…大部分与其相关的应用模块。重点讲解开发过程中如何更准确的体现用户交互性的便捷和高效,同时也帮助分析Designers和Developers在开发期间所要面对的问题。

通过Intent启动程序来查看文件

操作系统都有一套”血缘关系“列表,它用于标识所有可识别的类型文件的查看方式,例如:Mp3 ->Windows Media Player、Txt -> Notepad、JPG -> Picasa等等。相同的,在Android中也提供了这样一种机制,当用户想查看存储器中的某些文件时,将通过Intent找到启动这种类型文件的程序。
这里提供两个用于查看MP4和MP3的例子作为参考:

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(“/sdcard/test.mp4″);
intent.setDataAndType(Uri.fromFile(file), “video/*”);
startActivity(intent);

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(“/sdcard/test.mp3″);
intent.setDataAndType(Uri.fromFile(file), “audio/*”);
startActivity(intent);

文章来源 androidsnippets.org