Android – examination lab 1
Table of Contents
Android – examination lab 1...1
Reporting the examination...1
References...1
1.1 Android Development Installation...1
1.2 Hello Android with TTS...2
1.3 Eclipse IDE problems and solutions...5
1.4 Android AVD preparations...5
Reporting the examination
Hand in the working programs source code from 1.2 in fronter.
References
Android developers - https://developer.android.com where everything is explained. There is an Android user guide at: https://support.google.com/nexus
The Android Apps Development Tutorial for Beginners - 2015 is what I recommend you to start watching for the installation of Android Studio etc.
It is available in short episodes here: https://www.thenewboston.com/videos.php? cat=278.
I have put the above material in a bundled format here: http://users.du.se/~hjo/cs/com-mon/. The folder have some additional stuff as well which you can examine.
The bundled recordings are also available on YouTube here: https://www.youtube.-com/playlist?list=PLOJrc9IhPiwU9Iu_duwDxzTx-2WMHGNq4.
Lars Vogels Android Tutorials - http://www.vogella.de/android.html which also have very good instructions for everything about Android.
1.1 Android Development Installation
Follow an installation tutorial (see references) for the Android SDK and the IDE you choose, or some other tutorial.
1.2 Hello Android with TTS
Part 1
First implement a Hello Android program with an unique text in a TextView as done in the lecture presentation.
Part 2
Then extend the program with TTS (Text To Speech) functionality so the program can say something fun.
Your Hello Android Activity class need to imlement this interface:
implements TextToSpeech.OnInitListener
You need to add these member variables in your Hello Android Activity class:
private static TextToSpeech mTts = null;
private final int MY_DATA_CHECK_CODE = 2323;
Put the code below in public void onCreate(Bundle savedInstanceState)
// init TTS and read the default welcome message if (mTts == null)
checkTTS();
Add the following methods in the Hello Android Activity class:
// send an intent (message) to an activity to check if TTS is available private void checkTTS()
{
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}
// the TTS check result, if TTS is available
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance which automatically will call the
// TextToSpeech.OnInitListener when the TextToSpeech engine has initialized. mTts = new TextToSpeech(this, this);
}
else
{
// missing data, install it
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); startActivity(installIntent);
} }
}
// TextToSpeech.OnInitListener that will be called when the TextToSpeech engine has initialized.
@Override
public void onInit(int status) {
// TODO Auto-generated method stub if (status == TextToSpeech.SUCCESS) {
mTts.setLanguage(Locale.US);
String myText1 = getResources().getString(R.string.hello_android); String myText2 = "Welcome to the IK1058 course. See you in fronter!";
mTts.speak(myText1, TextToSpeech.QUEUE_FLUSH, null);
mTts.speak(myText2, TextToSpeech.QUEUE_ADD, null); }
else
Toast.makeText(this, "An error occurred with TTS init!", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
if (mTts != null)
mTts.shutdown();
super.onDestroy(); }
Part 3
When part 2 works extend the program with an EditText and a Button so it can speak the text you enter in the EditText when you press the Button.
In the main.xml layout add:
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus /> </EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button1" />
private Button mBtnTts;
private EditText mETxt1
Put the code below in public void onCreate(Bundle savedInstanceState)
// create controls
mBtnTts = (Button) findViewById(R.id.button1);
mETxt1 = (EditText) findViewById(R.id.editText1);
// listen for button press and speak the text in the edittext control mBtnTts.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
if (mETxt1.getText().toString().length() > 0 && mTts != null)
mTts.speak(mETxt1.getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
1.3 Eclipse IDE problems and solutions
It is important that you understand the Eclipse IDE on a basic level to continue. Things as different perspectives, Java, Debug, DDMS (Dalvik Debug Monitor System) etc. If you got problems with a perspective you can reset it.
When you compile the programs and Eclipse complains on that the R.java file is not found you may have to build the project a couple of times and possibly clean it as well.
If the Project > Build All/Project alternative not is possible to execute just disable the Project > Build Automatically choice.
Below the Source menu there are some real nice functions for the program coding. Examples:
Correct indentation - Ctrl + I
Format - Ctrl + Shift + F, which fixes ugly code so it looks nice.
If you press Organize Imports - Ctrl + Shift + O, Eclipse will automatically import all the needed packages for your source code.
Sometimes import android.R; or other packages are imported into your source code which
usually is a mistake since Eclipse may have been confused, especially when copy and pasting code. These import statements can therefore safely be removed from your source code. To force IntelliSense press Ctrl + Space at certain places and you will get the pop-up list with syntax and method proposals.
Try out some of the SDK samples in C:\android-sdk-windows\samples\android-x if you want. You need to create a new project from existing sample in the New Android Project dialogue in this case.
If you like me are missing toolbar buttons for undo and redo in the Eclipse editor an
undoredo_1.0.0.jar file is available in the labs folder which you can put in the eclipse\dropins folder. The source for this solution: http://stackoverflow.com/questions/819846/how-to-add-undo-redo-buttons-to-toolbar-in-eclipse.
1.4 Android AVD preparations
Note! The pictures in the instructions may display API8 (Android 2.2), you should use a newer API.
Create an AVD (Android Virtual Device) which is preferably done via you IDE. Configure the virtual phones Target and Hardware as your liking. You need a SD Card with some MB on and Google APIs and GPS support for later labs. See image 1.
I recommend to use a smaller display skin since the emulator window will be rather big otherwise. This can however be adjusted at start via the Launch Options dialogue and Scale display to real size or by just edit the AVD and change skin.
Skin table in pixels
QVGA is 320x240 (Quarter VGA)
WQVGA400 is 400x240) (Wide quarter VGA) WQVGA432 is 432x240) (Wide quarter VGA) HVGA is 320x480 (Half VGA)
WVGA800 is 800x480 (Wide VGA) WVGA854 is 854x480 (Wide VGA)
QHD is 960 x 540 (Quarter High Definition)
WSVGA is 1024x600 (Wide Super Video Graphics Array)
WXGA720 is 1280x720 (Wide eXtended Graphics Array)
Image 1.
Having the windows\tools” and
“[drive]:\android-sdk-windows\platform-tools” in your environment Path may also be needed for everything to work correct. In these folders we have many of the tools other programs use and we may use directly ourselves as for example: ADB (Android Debug Bridge), android.bat (Android SDK and AVD manager) etc.
1. Switch to DDMS view.
2. Goto the Emulator Control tab.
3. Then mark an emulator under devices which usually is 5554 for the first emulator, then put in the incoming number and press Call or fill in SMS and press Send.
As you can see in image 4 you can browse certain parts of the phones file system. You can for example access the SD Card.
You can pull, push and delete files from/to the phones memory with the 3 buttons located just above the text Info in the File Explorer tab in DDMS (see image 4) .
Image 3.
Image 4.
Either use adb.exe install -r <file.apk> to install the application to /data/app. If you got a physical phone attached via USB use: adb -d install -r <application filename>.apk
Or install the .apk with a file manager as ES File Explorer or Astro File Manager.