Changes between Version 7 and Version 8 of 2012/Projects/ASL/Tutorial1


Ignore:
Timestamp:
Aug 7, 2012, 3:20:07 PM (5 years ago)
Author:
xianyi
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • 2012/Projects/ASL/Tutorial1

    v7 v8  
    5959Place this under the winlab.tutorial package. 
    6060 
    61  
     61Press next after this. 
     62 
     63[[Image(Tutorial2.png)]] 
     64 
     65At the time of this tutorial writing, the Eclipse used here did not contain templates for BlankActivity. If you are using Eclipse and it autocreates the Activity and the manifest you can skip the next few steps but if not, it's a simple enough fix. 
     66 
     67So given the current problems, uncheck "Create Activity" and just hit "Finish". 
     68 
     69[[Image(Tutorial3.png)]] 
     70 
     71Again, if Eclipse can autocreate your activity file without errors then you can skip this next step. 
     72Expand "Tutorial1" in the Package Explorer on the left hand side. Then, right click the "src" folder and navigate to New -> Class and click on it. 
     73 
     74[[Image(Tutorial4.png)]] 
     75 
     76We will give it the name "Tutorial1Activity".  
     77 
     78Don't forget to place its package under winlab.tutorial.tutorial1 like we did when we created this project. 
     79 
     80Click "Finish" and move on. 
     81 
     82[[Image(Tutorial5.png)]] 
     83 
     84Afterwards, we will be introduced to our class. Since we want to write a simple executable application to test how the library works, we need to first modify this class and make it "Android ready". 
     85 
     86Quickly rewrite the code like below to finish our manual creation of the Activity for the tutorial: 
     87 
     88{{{ 
     89 
     90package winlab.tutorial.tutorial1; 
     91 
     92import android.app.Activity; 
     93import android.os.Bundle; 
     94 
     95public class Tutorial1Activity extends Activity { 
     96 
     97        /** Called when the activity is first created. */ 
     98        @Override 
     99        public void onCreate(Bundle bundle) { 
     100                super.onCreate(bundle); 
     101                setContentView(R.layout.main); 
     102 
     103        } 
     104 
     105} 
     106 
     107}}} 
     108 
     109Don't worry about any errors. Like I mentioned before, this is still due to the fact that Eclipse can't (or won't?) autogenerate an activity file. 
     110 
     111Right now, the class should appear as below: 
     112 
     113[[Image(Tutorial6.png)]] 
     114 
     115The main.xml file doesn't exist and neither has the activity been set as the main under the AndroidManifest.xml. Take this time now to insert the permissions and services from above as well as set Tutorial1Activity as the activity to execute when the program runs. 
     116Double click "AndroidManifest.xml" to open it up: 
     117 
     118[[Image(Tutorial7.png)]] 
     119 
     120Click on "!AndroidManifest" in the bottom right corner. This will bring up the XML editor. 
     121 
     122The current code needs to be changed to the one seen below: 
     123 
     124{{{ 
     125<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     126    package="winlab.tutorial.tutorial1" 
     127    android:versionCode="1" 
     128    android:versionName="1.0" > 
     129 
     130    <uses-sdk 
     131        android:minSdkVersion="8" 
     132        android:targetSdkVersion="15" /> 
     133 
     134    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
     135    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
     136    <uses-permission android:name="android.permission.INTERNET" /> 
     137    <uses-permission android:name="android.permission.BLUETOOTH" /> 
     138    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
     139    <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
     140    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
     141    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
     142    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 
     143 
     144    <application 
     145        android:icon="@drawable/ic_launcher" 
     146        android:label="@string/app_name" 
     147        android:theme="@style/AppTheme" > 
     148        <activity 
     149            android:name=".Tutorial1Activity" 
     150            android:label="@string/app_name" 
     151            android:windowSoftInputMode="stateHidden" > 
     152            <intent-filter> 
     153                <action android:name="android.intent.action.MAIN" /> 
     154 
     155                <category android:name="android.intent.category.LAUNCHER" /> 
     156            </intent-filter> 
     157        </activity> 
     158 
     159        <service 
     160            android:name="winlab.file.RunningService" 
     161            android:enabled="true" /> 
     162        <service 
     163            android:name="winlab.sql.Sensors_SQLite_Service" 
     164            android:enabled="true" /> 
     165        <service 
     166            android:name="winlab.sensoradventure.gps.GPSloggerservice" 
     167            android:enabled="true" /> 
     168    </application> 
     169 
     170</manifest> 
     171 
     172}}} 
     173 
     174Where we have added Tutorial1Activity as an activity under application, the required services under application, and the permissions used by the library. 
     175 
     176Save this and exit out. 
     177 
     178Now we need to import the library. Assuming the library has been saved onto the computer, right click "Tutorial 1", go to Build Path->Add External Libraries: 
     179 
     180 
     181[[Image(Tutorial8.png)]] 
     182 
     183ASL.jar should appear under "Referenced Libraries" as seen below. 
     184 
     185[[Image(Tutorial9.png)]] 
     186 
     187With the library added to the project, we can move onto designing a simple two button GUI to test to see if we can use it to log data from the Android device. 
     188 
     189Underneath res->layout, we will right click layout and go to New->Android XML File: 
     190 
     191[[Image(Tutorial10.png)]] 
     192 
     193Select a LinearLayout, name the file "main", and click Finish: 
     194 
     195[[Image(Tutorial11.png)]] 
     196 
     197Drag and drop two Buttons onto the Layout: 
     198 
     199[[Image(Tutorial12.png)]] 
     200 
     201Go to the main.xml at the bottom of the editor and change the xml as below: 
     202 
     203[[Image(Tutorial13.png)]] 
     204 
     205Close main.xml and return to the main activity. We will now implement the main activity: 
     206 
     207{{{ 
     208package winlab.tutorial.tutorial1; 
     209 
     210import winlab.ASL.AndroidSensors; 
     211import android.app.Activity; 
     212import android.os.Bundle; 
     213import android.view.View; 
     214import android.view.View.OnClickListener; 
     215import android.widget.Button; 
     216 
     217public class Tutorial1Activity extends Activity { 
     218 
     219        private Button start, stop; 
     220        private AndroidSensors androidSensors; 
     221 
     222        /** Called when the activity is first created. */ 
     223        @Override 
     224        public void onCreate(Bundle bundle) { 
     225                super.onCreate(bundle); 
     226                setContentView(R.layout.main); 
     227 
     228                start = (Button) findViewById(R.id.start); 
     229                stop = (Button) findViewById(R.id.stop); 
     230 
     231                start.setOnClickListener(startClick); 
     232                stop.setOnClickListener(stopClick); 
     233                stop.setClickable(false); 
     234 
     235                androidSensors = new AndroidSensors(this); 
     236                androidSensors.setAccelerometer(true); 
     237                androidSensors.writeToFile(true); 
     238                androidSensors.setAccelerometerRate(1); 
     239                androidSensors.prepareForLogging(); 
     240 
     241        } 
     242 
     243        private OnClickListener startClick = new OnClickListener() { 
     244 
     245                public void onClick(View arg0) { 
     246 
     247                        androidSensors.startLogging(); 
     248                        start.setClickable(false); 
     249                        stop.setClickable(true); 
     250 
     251                } 
     252 
     253        }; 
     254 
     255        private OnClickListener stopClick = new OnClickListener() { 
     256 
     257                public void onClick(View arg0) { 
     258 
     259                        androidSensors.stopLogging(); 
     260                        stop.setClickable(false); 
     261                        start.setClickable(true); 
     262 
     263                } 
     264 
     265        }; 
     266 
     267} 
     268}}} 
     269 
     270The library is introduced by the class AndroidSensors. To begin logging one sensor, we set the accelerometer to "true"; we set its update rate = 1ms, and we prepare the library for logging.  
     271 
     272Very simple application. Once it