简体   繁体   English

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

[英]What exactly does using the Application Context mean?

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. 我的书说我可以使用getApplicationContext()方法检索进程的应用程序上下文。 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? 另外我还会在main.xml中编写Activity Callbacks? 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." “在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. 我正在使用Android SDK和Eclipse。 I have made the Hello World application, but I have no idea what to do with Context or Retrieving resources. 我已经创建了Hello World应用程序,但我不知道如何处理Context或Retrieving资源。 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. 需要上下文时,请将活动对象用作上下文。

The callbacks you talk about are on the Activity class. 您谈到的回调都在Activity类上。 The Application Fundamentals describes what an Activity is: http://developer.android.com/guide/topics/fundamentals.html#Components 应用程序基础知识描述了一个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). 只有当您需要在Activity类(或其他组件)的生命周期之外存在的Context时,才能使用getApplicationContext()。 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 例如,这个是Android文档的一部分: http//android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html

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. 如果你正在使用Sam's Teach Yourself ...的第3小时书,那么你需要打开src\\com.androidbook.droid1\\DroidActivity.java文件。 In general, you would need src\\<package-name>\\<class-name>.java . 通常,您需要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. 当您打开该文件时,您将看到一个扩展Activity的类(在本例中为DroidActivity)并且已经具有onCreate()回调方法。 Anything that you want to happen during onCreate() goes inside that method. 你想在onCreate()期间发生的任何事情都进入那个方法。 Other callback methods can be added inside the activity class. 可以在activity类中添加其他回调方法。 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. 例如,您可以将其声明为活动类中的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. 如果关于方法属于何处,何处以及如何定义变量或常量,如何调用方法,如何使用类等等,那么最好在开始使用Android之前先阅读介绍性Java文本。 There are plenty of free resources available for that. 有足够的免费资源可供使用。

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

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