使用Application Context是什么意思? [英] What exactly does using the Application Context mean?

查看:401
本文介绍了使用Application Context是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新,对不起,如果这是一个非常愚蠢的问题。我只是想澄清一些事情。我的书说我可以通过使用 getApplicationContext()方法来检索进程的应用程序上下文。我只是真的不知道在哪里输入这个或什么做任何它。我可以去层次结构,但是我该如何处理所有脚本。还要在main.xml中写入Activity Callbacks?一个练习要我给我的项目添加一个日志记录,但是我不知道该怎么做。确切的文字说:

I'm new to this and I'm sorry if this is a really dumb question. I'm just trying to clarify things. My book says I can retrieve application context for process by using the getApplicationContext() method. I just really don't know where to type this or what to do with any of it. I can go to the hierarchy but what do I do with all the script there. Also where would I write Activity Callbacks, in the main.xml? An exercise wants me to add a logging tag to my project but I'm not sure how to do this. The exact text says:


在onCreate()回调方法中,使用Log.i()方法添加一个信息性日志消息。

"Within the onCreate() callback method, add an informational logging message, using the Log.i() method."

另一个练习说:


除了onCreate()之外还实现了一些Activity回调方法,比如onStart(),向每个回调方法添加一个日志消息,然后正常运行应用程序。

"Implement some of the Activity callback methods in addition to onCreate(), such as onStart(). Add a log message to each callback method and then run the application normally".

由于这些似乎是基本问题,有人可以帮助我。

As these seem like basic questions, can someone please help me.

我正在使用Android SDK和Eclipse。我已经做了Hello World应用程序,但我不知道如何处理上下文或检索资源。请帮助!

I am using the Android SDK, and Eclipse. I have made the Hello World application, but I have no idea what to do with Context or Retrieving resources. Please help!

推荐答案

我会给你的第一条规则:如果你不知道你为什么需要它,你可能会不需要它在需要上下文时,使用您的活动对象作为上下文。

The first rule I would give you: if you don't know why you need it, you probably don't need it. Use your activity object as the Context when you need a context.

您谈论的回调是在Activity类上。应用程序基础知识描述了一个活动: http://developer.android.com/指导/主题/ fundamentals.html#组件

The callbacks you talk about are on the Activity class. The Application Fundamentals describes what an Activity is: http://developer.android.com/guide/topics/fundamentals.html#Components

只有当您需要存在于生命周期之外的上下文时,才需要使用getApplicationContext()的Activity类(或其他组件)。您将需要查找有关特定情况的文档,这些情况需要,有很多浮动。例如,这是Android文档的一部分: http:// android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html

The only time you want to use getApplicationContext() is when you need a Context that exists outside of the lifecycle of an Activity class (or other component). You'll want to find documentation on specific cases where this is desired, there is a lot floating around. For example this one is part of the Android documentation: http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html

这篇关于使用Application Context是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆