【发布时间】:2015-09-04 16:38:22
【问题描述】:
我正在尝试学习如何开发 Android 应用程序。我正在阅读 Android 开发人员指南网站 http://developer.android.com/guide/components/intents-filters.html,特别是关于 Explicit 与 Implicit Intents 的内容。显式意图的示例之一如下所示:
// Executed in an Activity, so 'this' is the Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData(Uri.parse(fileUrl));
startService(downloadIntent);
构造函数 Intent(this, DownloadService.class) 看起来像 Android API 中的这个公共构造函数 (http://developer.android.com/reference/android/content/Intent.html):
public Intent (Context packageContext, Class<?> cls)
Intent 构造函数文档指出:
为特定组件创建意图。所有其他字段(操作、数据、类型、类)都是空的,尽管稍后可以通过显式调用对其进行修改。这提供了一种方便的方法来创建旨在执行硬编码类名的意图,而不是依赖系统为您找到合适的类;请参阅 setComponent(ComponentName) 了解有关此影响的更多信息。
我想弄清楚的是,构造函数中的“this”是什么?我的意思是,我知道一般的“this”是什么(您的活动的当前实例),但是在这个特定的内容中它是什么?在 cmets 中,它说“在 Activity 中执行,所以 'this' 是上下文”,但“上下文”是什么?它不是构成意图的五种信息之一(组件名称、操作、数据、类别、附加信息、标志)。我知道“DownloadService.class”参数是组件名称,所以我只是想弄清楚它是什么。
【问题讨论】:
-
好吧
Activity扩展了Context。来自文档It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
标签: android