原文来自Android SDK文档中的 docs/resources/articles/can-i-use-this-intent.html
Android提供了一个强大易用的Intent消息类型。 可以使用Intent 让应用成为库, 让代码模块化、可重用。比如,Home screen和AnyCut,就大量使用Intent来创建快捷方式(注:???)。
虽然Intent使用松耦合的API是一种好方式, 但是不能保证你发出的Intent 一定可以被别的应用接收, 尤其对第三方应用来说。 比如, Panoramio和它的RADAR Intent
本文主要讨论如何判断系统是否可响应我们发出的Intent。 下面的例子展示了一个通过查询系统Package Manager以确定是否有应用可以响应特定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 The Intent action to check for availability.
*
* @return True if an Intent with the specified action can be sent and
* responded to, false otherwise.
*/
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
可以这样使用上述辅助方法:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
final boolean scanAvailable = isIntentAvailable(this,
"com.google.zxing.client.android.SCAN");
MenuItem item;
item = menu.findItem(R.id.menu_item_add);
item.setEnabled(scanAvailable);
return super.onPrepareOptionsMenu(menu);
}
在这个例子中, 如果Barcode Scanner应用没安装那么相应的menu会被禁用。
另外一个更简单的办法是捕获调用startActivity()方法时可能抛出的ActivityNotFoundException, 但是这个办法只能让你在发出异常时进行处理, 而不能事先采取办法防止用户执行某些会引起错误的操作。这个技巧还可以用于应用启动的时候提示用户是否要安装某些未安装的应用, 然后使用适当的URI简单地重定向到Android Market(注: Google Play)
分享到:
相关推荐
This chapter shows how you can use LINQ to solve several data analysis tasks. I hope you find this chapter enjoyable, because the examples are really interesting. • Chapter 9: Interaction with the ...
The primary intent of this release is for entertainment and educational purposes, but the GPL does allow commercial exploitation if you obey the full license. If you want to do something commercial ...
It has never been my intent to make lots of money from this book. I was never in a hurry to finish this book because that rush could have compromised the quality and the coverage of its contents. In ...
APIs are themselves relatively high-level, the intent is to facilitate an implementation that can directly use the most efficient operations of the underlying platform. (Source: ...
My primary intent in writing this book is to provide the reader with basic programming, financial, and mathematical tools that can be successfully leveraged both in industry and academia. I cover the ...
I never thought that the first edition of this book would sell so long. But now, after twelve years, it’s time for a new edition that covers C++11, the new C++ standard. Note that this means more ...
to more than 1,100 pages, I still can’t present the C++ standard library as a whole. The library part of the new C++11 standard alone now has about 750 pages, written in very condensed form without ...
this can’t be done in a vacuum, so we provide simple examples that illustrate the technology and then walk you through the steps necessary to configure things such as deployment descriptors, access ...
The primary intent of this release is for entertainment and educational purposes, but the GPL does allow commercial exploitation if you obey the full license. If you want to do something commercial ...
Anybody can start building simple apps for the Android platform, and this book will show you how! Recently updated to include Android Jelly Bean, Android Apps for Absolute Beginners, Second Edition ...
Use Filer, Map and Reduce functions to express the intent behind code clearly and safely Build new functions from existing functions without necessarily writing any new functions, using point-free ...
The intent of this book is to provide examples of SQL usage that can be understood simply by reading. Second, topics are organized in an intuitive and logical sequence. SQL keywords are introduced ...
The boot.ini option /3GB was created for those cases where systems actually support greater than 2 GB of physical memory and an application can make use of it This capability allows memory intensive ...
Introduction IT organizations are realizing the benefits of infrastructure ... Mastering the topics in this book will ensure that you can approach any WAAS design project with confidence. xx Deployin
By proceeding to use this tool, you agree not to hold it's author accountable for any damages that could potentially arise from it's use or misuse. --------------------------------------------------...
GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 ... 51 Franklin Street, ...Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; ...
can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the ...
What’s Your Intent? Pieces of Intents Intent Routing Stating Your Intent(ions) Narrow Receivers The Pause Caveat ■Chapter 18: Launching Activities and Subactivities Peers and Subs Start ’Em Up...
You may use this version of the app anywhere and redistribute it. I have only tested on e3d, but it should work everywhere. I have heard it working on thunderbolt and e4g so far. Just make sure to ...
Any use of the term “UML 2” or “UML” in this specification, unless otherwise noted, will refer to UML 2.5 in general and the UML 2.5 specification in particular. Since SysML uses UML 2.5 as its ...