Android的意图语境混淆语境、意图、Android

2023-09-05 06:01:52 作者:一人摁全场

有人可以解释这对我好吗:

Can somebody explain this to me please :

Intent intent = new Intent(Context, AlarmReceiver.class);

我永远无法理解,我认真地想,我永远也不会,如果有人不试图解释这对我进行深入探讨。这整个范围内的事情是如此混乱给我。有时,它的工作原理是这样的:

I never understood and I seriously think I never will if somebody doesn't try to explain this to me in depth. This whole context thing is so confusing to me. Sometimes it works like this :

Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);

有时它不会工作一样,但只接受它:

Sometimes it wont work like that but it accepts only :

    Intent intent = new Intent(context, AlarmReceiver.class);

有时它:

Intent intent = new Intent(this, AlarmReceiver.class);

等。等,以及其他许多。

etc. etc. and many other.

我理解上下文的基础知识,但究竟有多少?为什么不蚀把我的错误一次,其他时间的优良?为什么我们有时需要申报方面?

I understand basics of context but how many are there? And why does eclipse throw me an error once and another time its fine? Why do we need to declare context sometimes? :

Context context;

我无法找到合适的情况下适用于所有情况,我怎么知道什么是正确的,在任何情况下?

I'm unable to find the right context for all situations how do I know what is the right one in every situation?

推荐答案

首先,让我来解释一下的 是一个好一点的环境,然后让我们去到它如何被使用和接收。从本质上讲,上下文是一个引用链接你的资源,你的程序。每个对象被赋予它自己的上下文,其中包含以设置对象上所需的资源。它需要被创建许多对象,并获取节目识别信息,以及其他目的。这使得它非常宝贵的建立新的观点和活动,但它也可以用于其它目的。另请参见这个答案了解详情。

First of all, let me explain what the context is a bit better, then let's go on to how it can be used and received. Essentially, context is a reference to linking your resources to your program. Each object is given its own context, which contains the resources required to set that object up. It is required for many objects to be created, and to get program identifying information, among other purposes. This makes it invaluable to set up new views and activities, but it can also be used for other purposes. See also this answer for more information.

的上下文中的项目可以来自各种各样的地方。有时它被存储并具有被检索,有时它被继承。基本上,这是面向对象的程序设计。

The context for an item can come from a variety of places. Sometimes it is stored and has to be retrieved, sometimes it is inherited. Basically, this is object oriented programming.

只给你举几个例子:

活动的继承方面。因此,如果你是在一个活动,你只需要通过自身使用环境。它还包含一个指向 getBaseContext()。你可能偶尔需要引用,如果你需要在整个应用程序上下文,但很有可能你不会一会儿。

Activity inherits context. Thus, if you are in an activity, you only need to pass itself to use the context. It also contains a pointer to getBaseContext(). You might occasionally need to reference that, if you need the entire application context, but most likely you won't for a while.

视图不继承上下文。但是,它确实有一个方法getContext().如果你需要从视图的背景下,这是方式得到它。这方面将是不完整的,但只会有上下文浏览的内容。

View does not inherit context. However, it does have a method getContext(). If you need to get a context from a view, this is the way to get it. This context will not be complete, but will only have the context for the contents of the View.

片段也不会继承范围内。它们包含的方法getActivity(),其中,如果片段是活动的,将返回的活性,这是上下文片段

Fragments also do not inherit context. They contain a method getActivity(), which if the Fragment is active, will return the activity, which is the context for the Fragment.

BroadcastReceivers不继承背景下无论是。事实上,它们不包含上下文可言,而是简单地接收当前上下文时接收到事件(如onReceive(Context背景,意图意图))

BroadcastReceivers do not inherit context either. In fact, they do not contain context at all, but simply receive the current context when an event is received (Such as onReceive(Context context, Intent intent))