简体   繁体   English

Android意图上下文令人困惑

[英]Android Intent Context Confusing

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);

Sometimes its : 有时它:

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? 为什么eclipse会一次又一次地给我一个错误呢? 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. 基本上,这是面向对象的编程。

Just to give you a few examples: 只是举几个例子:

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() . 它还包含一个指向getBaseContext()的指针。 You might occasionally need to reference that, if you need the entire application context, but most likely you won't for a while. 如果您需要整个应用程序上下文,您可能偶尔需要引用它,但很可能您暂时不会。

View does not inherit context. View不继承上下文。 However, it does have a method getContext() . 但是,它确实有一个方法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. 此上下文不完整,但只包含View内容的上下文。

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. 它们包含一个方法getActivity() ,如果Fragment处于活动状态,它将返回活动,该活动是Fragment的上下文。

BroadcastReceivers do not inherit context either. BroadcastReceivers也不继承上下文。 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) ) 实际上,它们根本不包含上下文,而只是在收到事件时接收当前上下文(例如onReceive(Context context,Intent intent)

Context Capabilities 上下文功能

The common actions you can safely take with a given Context object depends on where it came from originally. 您可以安全地使用给定的Context对象执行的常见操作取决于它最初的来源。 Below is a table of the common places an application will receive a Context, and in each case what it is useful for: 下面是一个应用程序将接收Context的常见位置表,并且在每种情况下它对以下内容有用:

在此输入图像描述

  1. An application CAN start an Activity from here, but it requires that a new task be created. 应用程序可以从此处启动活动,但它需要创建新任务。 This may fit specific use cases, but can create non-standard back stack behaviors in your application and is generally not recommended or considered good practice. 这可能适合特定的用例,但可以在您的应用程序中创建非标准的堆栈行为,通常不建议或认为是良好的做法。
  2. This is legal, but inflation will be done with the default theme for the system on which you are running, not what's defined in your application. 这是合法的,但通货膨胀将使用您运行的系统的默认主题完成,而不是在您的应用程序中定义的内容。
  3. Allowed if the receiver is null, which is used for obtaining the current value of a sticky broadcast, on Android 4.2 and above. 在Android 4.2及更高版本上,如果接收方为空,则允许用于获取粘性广播的当前值。

Original article here . 原文在这里

What i understand by means of context is environment.In simple terms context is the surroundings of anything.So when you are using any form of context you have to decide that what should be the surroundings of the things for which you are using context. 我通过上下文理解的是环境。简单来说,上下文是任何事物的环境。所以当你使用任何形式的上下文时,你必须决定你应该使用上下文的事物的环境。

For example if you want some data or field to remain through out the application you should define it in application class. 例如,如果您希望某些数据或字段保留在应用程序中,则应在应用程序类中定义它。

Now when you get application context in any of your components of your application,this field that you have declared in the application class will be in your context.Hence you can access it. 现在,当您在应用程序的任何组件中获得应用程序上下文时,您在应用程序类中声明的此字段将位于您的上下文中。您可以访问它。

Same is true for all context type. 所有上下文类型都是如此。

If you ever try using alertDialog in the service component by using context "this".Try this one and i bet you will surely get exception as "this" represent environment of service when used in it.And as it is background component we can't add window in that.Hence it will tell you the bad token exception.Which means token generated for surrounding view is not proper for alertDialog to display. 如果您尝试使用上下文“this”在服务组件中使用alertDialog。尝试这个并且我打赌您肯定会得到例外,因为“this”表示在其中使用时的服务环境。因为它是背景组件我们可以'在那里添加窗口。它会告诉你坏的令牌异常。这意味着为周围的视图生成的令牌不适合alertDialog显示。

Hope this gives you brief idea what you want. 希望这可以让你简要了解你想要什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM