DTU 
 

 

02162: Course on Software Engineering 2 (e14)

In this tutorial, you will learn how to implement a simple Android App and how to deploy and debug it on an Android phone via the USB debugging interface.

 

We will use the Android Development Tools for Eclipse (Eclipse ADT) for this purpose. In order not to mix up the Eclipse WTP with the Eclipse ADT, we install the Eclipse ADT as a separate installation of Eclipse (which is discussed below).

 

In this tutorial, you will implement a simple (mobile) app that reads and shows the absolute value of the accelerometer; more over the app starts an alert activity (screen), when a strong acceleartion (a shake) is detected.

 

Tasks

Here are the steps for the tasks of the third assignment:

 

  1. First you need to install the Eclipse ADT, possibly some drivers for your phone, and properly connect the phone to the Eclipse ADT as discussed below:

     

    1. First, you need to install the Eclipse ADT on your platform as discussed on http://developer.android.com/sdk/installing/installing-adt.html

       

      For the Windows platform, you will find a ready made bundle at http://developer.android.com/sdk/index.html#download. For other platforms, you need to first install another version of Eclipse and then install the Android SDK plugins as discussed here.

       

      Start the Eclipse ADT and select some workspace for it. If the "Devices" view is not open yet, open this view (Window -> Show View -> Other..., then select the view "Devices" from category "Android").

       

    2. Note that depending on the smart phone you are using, you also need to install some Android drivers for your phone (in order to be able to use the the USB debugging interface).

       

      For Windows and the Samsung Galaxy S5 phone that was made available to each group, you would need to install some special drivers (the Google drivers will not work). You will find the drivers and the instructions on how to install them at http://www.usb-drivers.org/samsung-android-usb-device-driver.html.

       

    3. Switch on the USB debugging mode on your phone. This can typically be done in the "Settings" (category "Applications" or "System") under "Development" or "Development options". In these options, select "USB debugging". Note that, on the Galaxy S5, the "Development options" are not visible in the intial configuration. In order to enable the "Development options" on the Galaxy S5, you need to go to the "About device" options in "Settings" (category "System") and click on "Build number" seven (!!) times in a row.

       

    4. Connect your smart phone to the USB port of your computer. Your phone might prompt you for an okay to allow USB debugging from the computer you connect to. Or it might prompt you for the mode in which you want to connect to the computer: in that case, select the USB debugging mode.

       

      If your phone is properly connected to the computer, your Eclipse ADT should show this phone in the "Devices" view. If it is not showing there, you either forgot or did not give the permission on the phone to connect in USB debgugging mode; or the Android driver for your phone is not properly installed on your computer.

       

      On the Windows platform, you can check whether your Android phone is properly connected by starting Window's "Device Manager" (Start -> Control Panel -> Device manager) after your smart phone is connected to the USB port. Check whether your device shows up in the category "Android USB devices". If it shows up in "Portable Devices" or "Other devices" only, you either forgot to switch on the USB debugging on the smart phone, or you need to install the drivers for the device (you can do this from the Device manager by clicking on the device, see above).

       

  2. Next, you should create a first app, which initially, just shows a text, such as "Hello world" and the current date.

     

    1. To this end, create a new "Android Application Project" in your Eclipse ADT workspace. You can call it "SE2 Hello World" and use a unique project name and an appropriate package name. When asked for creating an Activity, choose "Blank Activity with Fragment". If some errors appear in your workspace for the newly created project, try and clean the workspace (Project -> Clean).

       

    2. The above step, actually creates a functioning app already — except that it does not do much.

       

      In order to become familar with the structure of Android apps, you should do a minor change of the automatically generated app. In addition to the automatically generated text field (actually saying "hello world") in the main activity, you should add a second text field (in Adnroid called text view) to you app that shows the date and time when the app was started.

       

      There are different ways to do that. One way would be adding the text view programmatically. But, it is more in the spirit of Android apps to add the text field to the layout definitions of the app (see folder "res/layout"). If you created the app project with a "Blank Activity with Fragment", you find two files in there: activity_main.xml and fragment_main.xml. A double-click on the fragment_main.xml will open an editor where you can add a TextView widget with a graphical editor. After you added the TextView widget, have a look at the XML representation of the fragment_main.xml.

       

      The new text view has different layout attributes. It should also have an attribute android:id="@+id/textView1". The tag makes sure that the id with key "textView1" is created in an automatically generated class called R (which you can later use in your code to access the value of that id, which will be discussed below).

       

      When the activity is opened, the value of this text field should be set programmatically. This can be done in different ways. Here, we use the method onViewCreated() in the PlaceholderFragmet which is an inner class of the MainActivity in the package that you have chosen in the wizard. Override this method in order to set the value of the above text view to the current date. You can obtain the field programmatically by view.findViewById(R.id.textView1) where R is the automatically generated class making the values from the layout accessible in the Java code. Check whether the returned view is of type TextView and cast it accordingly. Then, you can use the method setText() to change the actuall text shown by that text field.

       

    3. Now, test your "SE2 Hello World App" by starting it on the phone. You do that by right-clicking on the project and selecting "Run As -> Android Application". Then a dialog for selecting a phone or an emulator (if installed) will beopended. Since the emulator is extremely slow, better chose your phone. If your phone does not show up, go back to the installation process (you need to install some drivers, connect your phone to the computers USB port, or switch on the USB debugging on your phone).

       

      You can also debug your app on the phone by choosing "Debug As".

       

    4. Make yourself a bit more familiar with the structure of Android projects and the use of the Eclipse IDE for developing and testing apps. To this end, have a look at the : Android "Getting Started" Tutorials and Android Development Tutorial of Vogella.

       

  3. In a next step, you will create an application that makes use of the acceleration sensor of the phone, and you will learn how to start another activity programmatically. When started, the app (its main activity) should show a text field that shows the current absolute value of the accelerometer (and update it whenever the value changes significantly). When the user shakes the phone, another activity (screen) should open. This activity should contain the information that a shake was detected.

     

    Below are the necessray steps for implementing such an app.

    1. Create another Android Application Project in your Eclipse workspace (as described above).

       

    2. Create another Blank Activity with a Fragment by using the respective Android wizard: On the project, select New -> Other... - > Android Activity (in category Android); in the dialog, select the "Blank Activity with Fragment" and continue (select some new names for the activity and its fragment, so that they do not collide with the names for the main activity).

       

    3. For the PlaceholderFragement of the main activity, implement and install a SensorEventListener when the view opens. You can do that by overriding the onViewCreated() method. If your sensor event listener is called AccelerometerEventListener you can install it by a code as follows:

       

        sensorManager = (SensorManager) this.getActivity().
           getSystemService(Context.SENSOR_SERVICE);
        accelerometerListener = new AccelerometerEventListener();
        sensorManager.registerListener(accelerometerListener,
           sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
           SensorManager.SENSOR_DELAY_NORMAL);

      The implementation of this listener should track changes of the accelerometer, and when the changes are big enough, update the text view showing the current absolute value of the acceleration — in order to access the textview, the listener could be initialized the textfield that it should update.

       

    4. In addition, the accelerometer listener should also start the new activity with a message saying that a shake was detected. Make sure that the accelerometer startes only one of these dialogs; and resumes updating the sensor and starting the alert activity only after the previous shake alert activity was closed.

       

      If the class of your alert dialog is called ShakeAlertActivity, you can initiate this activity from another activity by the following code snippet using the concept of intents:

       

        Intent intent = new Intent(activity, ShakeAlertActivity.class);
        activity.startActivity(intent);

       

  4. Have a look at other possible sensors of the Galasxy S5 and how they could be used and accessed for your project. Also have a look at clever algorithms to detect a shake more accurately (just looking for high acceleartions, is a bit crude).

     

    You also might want to change some colors (see more on colour resources here) in the alert activity (e.g. a red background).

     

Useful links

Here are some links that might be helpful:

 

 

Ekkart Kindler (), Sept 17, 2014