notes

21
Comp194-MA

Upload: peterbuck

Post on 19-May-2015

491 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Notes

Comp194-MA

Page 2: Notes

Introduction

Android includes a rich widget toolkit.

Cool ones: a Google Map, MediaPlayer, Photo Gallery, ect

The best way to figure out the widgets is to experiment with them.

These slides will walk through the GeoFlickr app.

Page 3: Notes

GeoFlickr

GeoFlickr retrieves the user’s current GPS coordinates and pulls flickr photos around that location.

The app uses Android’s Gallery widget to display the retrieved photos.

It also uses Android threads and handlers so the UI doesn’t freeze

Page 4: Notes

Screenshots

Home screen Loading Screen

Page 5: Notes

Screenshots

Photos outside MFA “onclick” behavior

Page 6: Notes

Juicy bits

GeoFlickr uses Android threads as well as the LocationManager service to determine GPS coordinates.

We’ll walk through how to set up LocationManager and throw out some snippets for threading.

Page 7: Notes

Location, Location, Location Location based services are some of

the most exciting applications of mobile apps.

The Android SDK exposes it’s GPS service through the LocationManager service.

Applications can either poll the service for updates or opt to receive updates when the device moves by a certain amount.

Page 8: Notes

Setup

Before an application can access location service it needs to set up permissions in the AndroidManifest file.

The Eclipse plugin provides a GUI to add the appropriate XML tags to the file.

Open “AndroidManifest.xml” and select the “Permissions” tab on the bottom.

Page 9: Notes

Setup contd.

Add the following as “Uses Permission” android.permission.ACCESS_MOCK_LO

CATION android.permission.ACCESS_COARSE_L

OCATION android.permission.ACCESS_FINE_LOC

ATION If you want to use sockets add:

android.permission.INTERNET

Page 10: Notes

Setting up DDMS

Since the emulator doesn’t have a physical GPS, the DDMS console allows developer’s to send fake GPS coordinates to the emulator.

To bring up DDMS in Eclipse: Window->Open Perspective->Other->DDMS

To enable LogCat logging in Eclipse: Window->Preferences->Android->DDMS and change the log level to info.

Page 11: Notes

DDMS

Send GPS coords

Logging

System info

Page 12: Notes

LocationManager code Register for location updates: LocationManager locationManager =

(LocationManager)getSystemService(Context.LOCATION_SERVICE);

flickrLocationReciever flReciever = new flickrLocationReciever();

locationManager.requestLocationUpdates( locationManager.getProviders(true).get(0), 5, 10, flReciever);

public class flickrLocationReciever implements LocationListener {

@Override

public void onLocationChanged(final Location arg0) {

// do things

}

}

Page 13: Notes

Gallery Adapters

Android’s gallery widget accepts an adapter object to facilitate rendering.

Adapter’s are flexible because they allow developer’s to respond to gallery events in a particular fashion.

They also allow developer’s to change how gallery items will render.

Page 14: Notes

Gallery Code

ImageAdapter ia = new ImageAdapter(this);

Gallery ga = ((Gallery) findViewById(R.id.imageGallery));

ga.setAdapter(ia);

public class ImageAdapter extends BaseAdapter {// override all the methods and do stuff

}

Page 15: Notes

Threading

Using threads for background tasks is important because it allows the UI to remain responsive.

Android implements threads using the standard Java threading model.

Events are raised using the “Message” class and handled using the “Handler” class.

Page 16: Notes

Threading code

Thread t = new Thread() {

public void run(){

// do stuff

// let the handler know we are done

handler.sendEmptyMessage(1);

}}

T.start();

private Handler handler = new Handler(){

public void handleMessage(Message msg) {

// handle the end of the thread

}}

Page 17: Notes

Playing With Google Maps

Android has a built in widget for the maps called MapView

MapView requires an API key which is based off something called “keystore”.

Since we are running in Debug environment we can use the debug.keystore that android creates us in ~/.andriod/debug.keystore usually

Page 18: Notes

Getting your Maps API Key First get your md5 finger print

daum@daum ~/.android $ keytool -list -alias androiddebugkey -keystore debug.keystore -storepass android -keypass android androiddebugkey, Jan 26, 2009, PrivateKeyEntry, Certificate fingerprint (MD5): B8:F5:D9:31:8D:19:C6:13:86:7F:4F:B3:A4:C9:A2:CC

Take the B8…….CC to http://code.google.com/android/maps-api-signup.html and get your API key

When you implement the MapView widget add android:apiKey=“YOUR KEY” to the widget

Page 19: Notes

Maps Require the Internet GoogleMaps requires that you have

access to the internet so your application needs permission to use the internet

Add the permission as a child under “manifest” in AndriodManifest.xml <uses-permission

android:name=“android.permission.INTERNET” />

Now you should be able to simply load the map

Page 20: Notes

Map Troubleshooting

If you see the Map but none of the actual tiles are loading on the map there are two possible problems You do not have the correct API key, make

sure you are using your debug one (or your production one if you are publishing the app)

Your application does not have access to the Internet

If you check both of the above problems, you most likely will end with a map that works

Page 21: Notes

A map with no tools is no Map at all Now we have a map, but we cannot click on

it, zoom in or out on it. To make it clickable you can in the MapView widget in the xml add android:clickable=“true”

Now you can click but we still cannot zoom. There is a zoom widget called ZoomControl! Get the ZoomControl from the MapView

Mapview.getZoomControls() Add it to the a linear layout at the bottom of the

map to make them visible linearLayout.addView(Mapview.getZoomControls()

Now we have a function map!