使用应用程序上下文到底意味着什么?

发布于 2024-10-20 22:06:31 字数 534 浏览 6 评论 0原文

我对此很陌生,如果这是一个非常愚蠢的问题,我很抱歉。我只是想澄清一些事情。我的书说我可以使用 getApplicationContext() 方法检索进程的应用程序上下文。我只是真的不知道在哪里输入这个或如何处理它。我可以转到层次结构,但我该如何处理那里的所有脚本。另外,我应该在 main.xml 中的哪里编写活动回调?一个练习希望我向我的项目添加一个日志记录标签,但我不知道如何执行此操作。确切的文字说:

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

另一个练习说:

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

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

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

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:

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

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".

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃颗糖壮壮胆 2024-10-27 22:06:31

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

您谈论的回调是在 Activity 类上。应用程序基础知识描述了什么是活动:http://developer.android.com/ guide/topics/fundamentals.html#Components

唯一需要使用 getApplicationContext() 的时候是当您需要一个存在于 Activity 类(或其他组件)生命周期之外的 Context 时。您需要找到有关需要此操作的特定情况的文档,周围有很多文档。例如,这是 Android 文档的一部分: http:// android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html

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

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

囚我心虐我身 2024-10-27 22:06:31

对于您在此处处理的任务,您将使用定义应用程序行为的 Java 代码,而不是定义资源和布局的 XML 文件或声明基本应用程序属性的 AndroidManifest.xml 文件。

如果您正在使用Sam's Teach Yourself...一书的第3小时,那么您需要打开src\com.androidbook.droid1\DroidActivity.java文件。一般来说,您需要src\\.java。当您打开该文件时,您将看到一个扩展 Activity 的类(在本例中为 DroidActivity),并且已经具有 onCreate() 回调方法。您希望在 onCreate() 期间发生的任何事情都会发生在该方法中。可以在活动类中添加其他回调方法。要查看包含所有生命周期回调(但不执行任何操作)的示例,请查看 这里

日志标签只是一个字符串。例如,您可以将其声明为活动类中的 private static final String

如果对方法所属的位置、在何处以及如何定义变量或常量、如何调用方法、如何使用类等等感到困惑,那么最好在开始使用 Android 之前先阅读介绍性 Java 文本。有大量可用的免费资源。

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.

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.

A logging tag is just a string. You can declare it, for example, as a private static final String inside the activity class.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文