Autor Wiadomo¶ć
WilliamHet
PostWysłany: ¦ro 18:02, 21 Cze 2017    Temat postu: Hamlet ?

?Android Software Progress
Chapter four. Underneath the Covers: Startup Code and Resources during the MJAndroid Software
Chapter 3 introduced the major software we use in such a book to illustrate fundamental Android concepts. That chapter explained which information make up the source code, nonetheless it didn’t actually cover any source code inside the software. We’ll get started trying at source code with this chapter. And to help you to definitely get started developing an software very quickly, we’ll begin with the number one task every standalone software has to perform: initialization.
The events covered with this chapter occur somewhere between your selecting “Run As Android Application” from the Eclipse menu and seeing the map that MJAndroid displays at startup. This chapter shows how Android makes it straight forward to develop relatively complex purposes. In just 80 lines of code and some associated XML resource data files, MJAndroid manages to:
Display an interactive map
Track the existing location on the Android phone and update the map
Construct a local databases of related information and load consumer preferences into it
Produce a dynamically changing menu
Display person interface factors these types of as labels, buttons, and spinners
Run a new Activity to display a supporting screen
The Java code in an Android software interacts tightly with XML resource information, so we’ll bounce again and forth involving them on this chapter. As we point out repeatedly, XML documents are easier to tweak during advancement and retain over the life of an software. The pattern of Android encourages you to definitely specify the glance and behavior within the software inside resource documents.
Initialization Parameters in AndroidManifest.xml
As Chapter 3 explained, we told Android to launch Microjobs.java given that the to start with Activity for MJAndroid. We defined that over the Software tab for the AndroidManifest.xml editor. The very first part on the XML code that benefits from that choice is revealed in this article:
This section of your chapter focuses within the XML on this file. The MicroJobs Activity is identified around the manifest for the beginning on the file. This part within the file is normally created in Eclipse after you very first generate the Challenge which you use to jot down your software .
Like all ideal XML documents, line 1 has the standard declaration within the XML version in addition to the character encoding put into use. Before we get into the Activities that make up the MJAndroid software, we define a small number of parameters and declare needed permissions for that whole software:
This is just the package name we gave when we created the software in Eclipse. It is also the default package for all the modules inside the software.
This is surely an integer that should always increment with every single new version of your software. Every software should include a version code, and it should always be a monotonically increasing integer from version to version. This allows other programs (like as Android Market, installers, and launchers) easily figure out which is the latest version of an software. The filename of your .apk file should include this same version variety, so it is obvious which version it possesses.
This version identifier serves as a string, and it is intended to be further like the version figures you usually see for purposes. The naming convention is up to you, but generally the idea is to work with a scheme like m.n.o (for as nearly all figures as you should use), to identify successive amounts of change to the software. The idea is the fact this is the version identifier that would be displayed to some consumer (either by your software or another software).
There are four of these in MJAndroid, and they declare that the software intends to employ functions of Android that require explicit permission from the person belonging to the cell gadget jogging the software. The permission is requested if the software is installed, and from then on Android remembers that the consumer stated it was OK (or not) to run this software and accessibility the secure functions. There are lots of permissions now defined in Android, all described while in the Android documentation (search for android.Manifest.permission ). You'll also define your have permissions and use them to restrict other applications’ obtain to functions in your own software, unless the person grants one other software that permission. The permissions requested in this article are:
ACCESS_FINE_LOCATION. which is required to obtain location data from the GPS sensor.
ACCESS_LOCATION_EXTRA_COMMANDS. The Android documentation doesn’t tell us which location commands are “extra,” so we’ll ask for all of these.
CALL_PHONE. This will allow MJAndroid to request that the Dialer spot a cell phone call on its behalf.
ACCESS_MOCK_LOCATION. so we can get fake location particulars when we’re working beneath the emulator.
INTERNET. so we can retrieve map tiles over an Internet relationship.
This is the filename for a PNG file that comprises the icon you’d like to utilize in your software. On this case we’re telling the Android SDK to glance for your icon file while in the drawable subdirectory for the res (resources) directory beneath MJAndroid. Android will use this icon for your personal software inside of the Android Desktop.
Turning our attention to the definition to the primary (and main) Activity, MicroJobs, we number one define a number of attributes for that Activity:
The name for the Activity. The extensive name with the Activity incorporates the package name (which in our software is “com.microjobsinc.mjandroid.MicroJobs”), but since this file is always employed while in the package’s namespace, we really don't need to get to include the leading package names. The Android SDK strips the package name down to “.MicroJobs” when it creates this part of AndroidManifest.xml . and even the leading period is optional.
The label that we hope to appear with the top within the Android screen if the Activity is within the screen. We saw this before in HelloWorld, where we changed the string in strings.xml to match our software.
We then declare an intent filter that tells Android when this Activity should be run. We talked briefly about Intents in Chapter 1. and now we see them in use. As you’ll recall, when Android encounters an Intent to fulfill, it looks among the on hand Activities and Providers to obtain something that can provider the Intent. We established two attributes:
Right now Android is trying to launch this software, so it is trying for an Activity that declares itself able to resolve the MAIN action. Any software that is definitely going to be launched by the Launcher needs to have exactly one particular Activity or Program that makes this assertion.
The Intent resolver in Android works by using this attribute to further qualify the Intent that it is shopping for. With this case, the qualification is the fact that we’d like for this Activity to be displayed from the Consumer Menu so the consumer can select it to begin this software. Specifying the LAUNCHER category accomplishes this. You will have a perfectly valid software without this attribute-you just won’t be able to launch it from the Android consumer interface. Normally, again, you’ll have exactly a person LAUNCHER for every software, and it will appear from the same intent filter since the opening Activity of your software.
The most advantageous content to your career. Discover unlimited learning on demand for close to $1/day .

Powered by phpBB © 2001, 2005 phpBB Group