Android Context Class更具体 [英] Android Context Class more specific
问题描述
在Android中使用Context类有何目的?请向我深入解释并更加具体.我阅读了所有其他文章,但这些文章都不够具体,无法让我清楚地理解.
我知道Content类可以访问特定于应用程序的资源和类,还可以调用应用程序级别的操作,例如启动活动,广播和接收意图等.
For what purpose Context class is used in android?Please explain me in depth and be more specific .I read all other posts but none of them were specific enough to give me clear understanding.
I know Content class 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.
就像这里,
Intent intent = new Intent(this,new_class.class);
Like Here,
Intent intent=new Intent(this,new_class.class);
为什么要将Main活动上下文传递给Intent构造函数调用.此活动上下文包含什么类型的信息,它将如何帮助它,向它提供什么类型的资源访问?(请提供示例).
相似地,
在这里,
TextView textview =新的TextView(this);
why we are passing Main activity context into the Intent constructor call.what type of information does this activity context contain,how will it help it ,what type of resource access is it providing to it ?(with example please).
Similarly,
here,
TextView textview=new TextView(this);
为什么TextView需要活动上下文?它如何帮助它.
Why TextView need activity context?How does it help it.
推荐答案
在Stackoverflow上已经有对Context
的一些很好的解释(请参阅链接的问题,并向我们提供搜索"功能.此外,Android的源代码是可以在grepcode.com
上找到,如果您真的有兴趣可以看一下自己.如果可以看看自己,为什么要相信别人的回答?;-)
There are already several good explanations of Context
on Stackoverflow (see the linked questions and us the "search" feature. Also, the source code for Android is available on grepcode.com
and you can look yourself if you are really interested. Why trust someone else's answer if you can look yourself? ;-)
但是,我会回答您的具体问题:
However, I will answer your specific questions:
Intent intent=new Intent(this,new_class.class);
为什么要将Main活动上下文传递给Intent构造函数 呼叫.此活动上下文包含什么类型的信息,如何 它会帮助它吗,它提供什么类型的资源访问 (请举个例子).
why we are passing Main activity context into the Intent constructor call.what type of information does this activity context contain,how will it help it ,what type of resource access is it providing to it ?(with example please).
在这种情况下(Intent
的2参数构造函数),Context
参数仅用于确定目标Activity
的程序包名称.假设您的应用程序的程序包名称是"com.example.app",并且MyActivity
是您的应用程序的活动",则以下代码段在功能上都是相同的:
In this case (the 2-argument constructor for Intent
), the Context
parameter is only used to determine the package name of the target Activity
. Assuming that the package name of your application is "com.example.app", and MyActivity
is an `Activity of your application, the following code snippets are all functionally identical:
Intent intent = new Intent(this, MyActivity.class);
Intent intent = new Intent(getApplicationContext(), MyActivity.class);
Intent intent = new Intent();
intent.setComponent(new ComponentName(this, "com.example.app.MyActivity");
Intent intent = new Intent();
intent.setComponent(new ComponentName(this, MyActivity.class);
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example.app", MyActivity.class);
Intent intent = new Intent();
intent.setClass(this, MyActivity.class);
Intent intent = new Intent();
intent.setClassName("com.example.app", "com.example.app.MyActivity");
类似地,在这里,
Similarly, here,
TextView textview =新的TextView(this);
TextView textview=new TextView(this);
为什么TextView需要活动上下文?它如何帮助它.
Why TextView need activity context?How does it help it.
所有View
都需要一个Context
.将Context
视为视图所有者".这可控制View
的生存期.通常,View
的生存期应与其拥有的Activity
相同,这就是为什么在创建View
时通常将Activity
作为Context
参数传递的原因.销毁Activity
时,所有拥有的View
也会被销毁.另外,View
使用Context
可以访问资源(可绘制图形,布局,字符串,主题等).
All View
s need a Context
. Think of the Context
as the "owner of the View". This controls the lifetime of the View
. In general, a View
should have the same lifetime as its owning Activity
, which is why you usually pass the Activity
as the Context
parameter when creating a View
. When the Activity
is destroyed, all of the owned View
s are also destroyed. Additionally, the View
uses the Context
to gain access to resources (drawables, layouts, strings, themes, etc.).
这篇关于Android Context Class更具体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!