eclipse 使用应用程序上下文究竟是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5228160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What exactly does using the Application Context mean?
提问by Chris P
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:
我是新手,如果这是一个非常愚蠢的问题,我很抱歉。我只是想澄清一些事情。我的书说我可以使用该getApplicationContext()
方法检索进程的应用程序上下文。我真的不知道在哪里输入这个或如何处理它。我可以转到层次结构,但是我如何处理那里的所有脚本。另外我应该在哪里写活动回调,在 main.xml 中?一个练习要我向我的项目添加一个日志记录标签,但我不知道如何做到这一点。确切的文字说:
"Within the onCreate() callback method, add an informational logging message, using the Log.i() method."
“在 onCreate() 回调方法中,使用 Log.i() 方法添加信息性日志消息。”
and another exercise says to:
另一个练习说:
"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".
《在onCreate()之外实现一些Activity的回调方法,比如onStart(),给每个回调方法添加一条日志信息,然后正常运行应用程序》。
As these seem like basic questions, can someone please help me.
由于这些似乎是基本问题,有人可以帮助我。
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!
我正在使用 Android SDK 和 Eclipse。我已经制作了 Hello World 应用程序,但我不知道如何处理上下文或检索资源。请帮忙!
回答by hackbod
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.
我要给你的第一条规则是:如果你不知道为什么需要它,那么你可能不需要它。当您需要上下文时,将您的活动对象用作上下文。
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
您谈论的回调在 Activity 类上。Application Fundamentals 描述了 Activity 是什么:http: //developer.android.com/guide/topics/fundamentals.html#Components
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
只有当您需要一个存在于 Activity 类(或其他组件)生命周期之外的 Context 时,才会使用 getApplicationContext()。您将需要找到有关需要这样做的特定情况的文档,有很多浮动。例如,这是 Android 文档的一部分:http: //android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html
回答by erichamion
For the tasks you're working with here, you'll be using the Java code that defines the behavior of the application, not the XML files that define resources and layouts or the AndroidManifest.xml file that declares basic application properties.
对于您在此处处理的任务,您将使用定义应用程序行为的 Java 代码,而不是定义资源和布局的 XML 文件或声明基本应用程序属性的 AndroidManifest.xml 文件。
If you're working with Hour 3 of the Sam's Teach Yourself...book, then you need to open the src\com.androidbook.droid1\DroidActivity.java
file. In general, you would need src\<package-name>\<class-name>.java
. When you open that file, you'll see a class (in this case, DroidActivity) that extends Activity and already has the onCreate()
callback method. Anything that you want to happen during onCreate()
goes inside that method. Other callback methods can be added inside the activity class. To see an example that has all the lifecycle callbacks (but doesn't do anything in them), look here.
如果您正在使用Sam's Teach Yourself...书的第 3 小时,则需要打开该src\com.androidbook.droid1\DroidActivity.java
文件。一般来说,您需要src\<package-name>\<class-name>.java
. 当您打开该文件时,您将看到一个扩展 Activity 并且已经具有onCreate()
回调方法的类(在本例中为 DroidActivity)。您想在此期间发生的任何事情onCreate()
都在该方法中。可以在活动类中添加其他回调方法。要查看包含所有生命周期回调(但不执行任何操作)的示例,请查看此处。
A logging tag is just a string. You can declare it, for example, as a private static final String
inside the activity class.
日志标记只是一个字符串。例如,您可以将其声明为private static final String
活动类的内部。
If there's confusion about where methods belong, where and how to define variables or constants, how to call methods, how to use classes, and so forth, then it might be best to go through an introductory Java text before starting with Android. There are plenty of free resources available for that.
如果对方法属于哪里、在何处以及如何定义变量或常量、如何调用方法、如何使用类等方面存在混淆,那么最好在开始使用 Android 之前阅读 Java 介绍性文本。有很多可用的免费资源。