公共意图(上下文packageContext,类cls)
参数
packageContext 实现此功能的应用程序包的上下文
班级。
cls 用于意图的组件类。
正如你所说
java.lang.Object
↳ android.content.Context //see this
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity // see this
您的课程扩展了“活动”。
public class YouActivity extends Activity // extends Activity
Hence this
引用 Activity Context 和意图的第一个参数是相同的。
检查此链接
http://startandroid.ru/en/lessons/complete-list/229-lesson-22-intent-intent-filter-context-theory.html http://startandroid.ru/en/lessons/complete-list/229-lesson-22-intent-intent-filter-context-theory.html
不过我会尽力解释得更好。
看一下源代码
3811
3812 public Intent(Context packageContext, Class<?> cls) {
3813 mComponent = new ComponentName(packageContext, cls);
3814 }
And then
61
62 public ComponentName(Context pkg, String cls) {
63 if (cls == null) throw new NullPointerException("class name is null");
64 mPackage = pkg.getPackageName(); // see this
65 mClass = cls;
66 }
And getPackageName()
是方法Context
.
public abstract String getPackageName ()
Added in API level 1
Return the name of this application's package.
Note
Intent intent = new Intent(this, DisplayMessageActivity.class);
这是明确的意图。您通常会使用显式意图来启动自己的应用程序中的组件,因为您知道要启动的活动或服务的类名称。
我添加了 Chris Stratton 的评论,因为他更好地解释了为什么需要上下文作为第一个参数。
有多种方法可以创建不需要上下文的 Intent。
但是如果你想针对特定包的特定类,那么
为目标包提供上下文是一种现成的方法。
请注意,上面的代码利用了目标类是这样的事实:
与发送者在同一个包中,因此发送者(“this”)减少时
到包上下文也将是正确的包上下文
目标。情况并非总是如此。
进一步阅读显式和隐式意图@
http://developer.android.com/guide/components/intents-filters.html http://developer.android.com/guide/components/intents-filters.html
并且还要检查
http://developer.android.com/training/basics/intents/sending.html http://developer.android.com/training/basics/intents/sending.html