Hardware Reference
In-Depth Information
res/values/strings.xml
The strings.xml file is a container for reusable data such as button and application names. The strings are static,
but could be uses throughout the program. It is good programming practice to define the strings in this file instead
of hard-coding them into other locations. In the manifest @string/app_name and @string/ToggleButton are used as
static variable containers, as shown in Listing 4-5. It is possible to replace the variable with the actual string value to
save on code length, but it is not recommended.
Listing 4-5. strings.xml Replacing the Autogenerated Original
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Mega ADK</string>
<string name="ToggleButton">ToggleButton</string>
</resources>
<!-- end of strings.xml -->
src/CH4.example.proArduino/CH4ExamplesActivity.java
Here is the heart and soul of an Android app. Listing 4-6 is broken up in to seven parts to help explain everything
that goes into the Java portion of Android applications. Listing 4-6 code appears in order of placement in the
CH4ExamplesActivity.java file and makes up the complete file located in the workspace under src/ch4.example.
proArduino . Most of the code for this example starts the basic framework to send data to the accessory board. Parts
3 through 6 are set up to be reusable. After we've finished this example, we'll set up a framework to perform two-way
communications that you can use in any project by changing the activity class and the package name. Aside from
the project name, package name, activity name, and accessory definition, manifest.xml , accessory_filter.xml ,
and Listing 4-6 remain relatively unchanged for new projects. Part 7 of Listing 4-6 is where you will change the code
between projects.
Part 1, line 1 of Listing 4-6 describes the package that the Java file belongs to. This is also the entry package that
was defined in the manifest file. The rest of the file imports needed functions and classes for the rest of the program.
Imports are mostly equivalent to C/C++ #include statements and inform the code what classes are needed. USB
communication is handled through a file, so the Java I/O file handlers need to be imported, but not all of the I/O
library classes are needed and only a subset of the classes are imported. The same is true for the android.*
libraries—only the ones that are actually needed are imported. It is possible to import every class in a library at once
with a wildcard character ( * ), as is done with the com.android.future.usb.* libraries. When adding objects to
the user interface, each object type will need to be imported from widgets in the same fashion as the toggle button.
Listing 4-6. CH4ExamplesActivity.java, Part 1 of 7
package ch4.example.proArduino;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
// Android components
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
 
Search WWH ::




Custom Search