How to Develop a Native Android App

quantityforeheadMobile - Wireless

Dec 10, 2013 (3 years and 4 months ago)

78 views

How to Develop a

Native Android App

Overview of Android Concepts
Map Overlays for Google Maps


CS406 Mobile Device Development
Roger Webster, Ph.D.


Roger.Webster@millersville.edu
D&E Communications Computer Science Wing
Caputo Hall
Dept. of Computer Science
Millersville University
Millersville, PA 17551
http://cs.millersville.edu/~webster/


Note: some images taken from google.com and apple.com


6/13/2012 1 Copyright ©2012 Dr. Roger Webster
All Rights Reserved

You
Are
Here!
Android GPS Map Overlays for Pinpointing
My car is here
Street Map

Driving
Directions to
Issacs
Restaurant
You
Are
Here!
Useful for pin pointing locations on Maps
Preparing for the Tutorial
• Must have Eclipse IDE installed
• Must have Android SDK installed
• Must have knowledge of Java
• Must have the external Google Maps library
installed in your SDK environment and in
AdroidManifest.xml.
• The Maps library is included with the Google
APIs add-on, which you can install using the
Android SDK and AVD Manager.
Get a Google Maps API Key
• A Google Maps API key is required to integrate Google Maps into your
Android application.
• To apply for a key:
• Locate the SDK debug certificate in the default folder of "C:\Documents and
Settings\<username>\Local Settings\Application Data\Android". The filename of
the debug keystore is debug.keystore.
• Copy the debug.keystore file to a folder named C:\Android\.
• Open the command window and navigate to C:\Program
Files\Java\<JDK_version_number>\bin to locate the Keytool.exe.

Execute the following to extract the MD5 fingerprint:


keytool.exe -list -alias androiddebugkey -keystore
"C:\Android\debug.keystore" -storepass android -keypass android

• Copy the MD5 certificate fingerprint and navigate your web browser to:
https://developers.google.com/android/maps-api-signup.
• Follow the instructions on the page to complete the application and obtain the
Google Maps key.

For more information on using Google Maps in Android application
development:

• https://developers.google.com/android/maps-api-signup

• debug.keystore will later be yourkey.keystore for Apps
on the Android Market, you will need a new Map API code
Get a Google Maps API Key
• Webster cheat -> use cmd tool prompts:

In cmd tool:

• pushd C:\Program Files (x86)\Java\jre6\bin
• keytool -list -alias androiddebugkey -keystore
C:\Users\webster\.android\debug.keystore -
storepass android -keypass android >
C:\Windows\Temp\keypass.txt
• popd
• pushd C:\Windows\Temp
• notepad keypass.txt
• Then go to
http://code.google.com/android/maps-api-
signup.html

Copy and paste and get key



Get a Google Maps API Key
Put this
In main.xml
Create an Android Virtual Device (AVD)
Defines the system image and device settings
used by the Emulator
To create an AVD in Eclipse:
1.Select Window > Android SDK and AVD Manager.
The Android SDK and AVD Manager displays.
2.Make sure the entry for Virtual Devices is selected
and click New.
Then Create new AVD window displays.
3.Enter a Name for the AVD.
4.Select Google APIs as the Target!!!!
5.Click Create AVD.
6.
Close the Android SDK and AVD Manager.

Create the Android Project
To create the project in Eclipse:
1.Select File > New > Project.
2.Select Android Project in the Android folder and
click Next.
3.Enter GPSSimulator as the Project Name.
4.Select Google APIs as the Build Target.
5.Enter GPSSimulator as the Application name.
6.Enter DrWebsterApps.android.gpssimulator as the
Package name.
7.Enter gpsSimulator as the Activity name.
8.Click Finish.
Use the Google Maps API
Google
Maps
API
Let’s take a
Look at
Main.xml
MapActivity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >



<com.google.android.maps.MapView

android:id="@+id/mapViewrww“
android:layout_width="fill_parent"
android:layout_height=" wrap_content "

android:apiKey="0UjsfpwwIojfd2k62JL269-wbFpDg5Sv3pdgI6g" />

<Button
android:id="@+id/clickBtnZout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Map and Driving Directions" />


</RelativeLayout>
Mapview component
Key comes from Google
Unique id for java code
If you don’t see a map
Double check Google
Maps API key
With Java



public class cs406GPSsimulator extends MapActivity {
MapView mapView;
MapController mc;
double latitude, longitude;
LocationManager lm;
LocationListener locationListener;
MyItemizedOverlay itemizedoverlay;
List<Overlay> mapOverlays;
Drawable drawable1, drawable2;
MyItemizedOverlay itemizedOverlay1, itemizedOverlay2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String s = (String) getResources().getString(R.string.app_name);
this.setTitle(s);

DrivingDirectionsButton = (Button) findViewById(R.id.clickBtnZout);
DrivingDirectionsButton.setOnClickListener(myDrivingDirectionsbuttonListener);


mapView = (MapView) findViewById(R.id.mapViewrww);
mc = mapView.getController();
mapView.displayZoomControls(true);
StartGPS();
}


Map Overlay Variables
MapActivity
Main.xml has MapView
Map Overlay Variables



private class MyLocationListener implements LocationListener {

public void onLocationChanged(Location loc) {
public void onLocationChanged(Location loc) {
GeoPoint p = new GeoPoint((int) (42.4710 * 1E6), (int) (-70.9216 * 1E6));

if (loc != null) {
p = new GeoPoint((int) (loc.getLatitude() * 1E6),(int) (loc.getLongitude() * 1E6));
latitude = loc.getLatitude();
longitude = loc.getLongitude();
if (firsttime) {
String msg = "latitude is " + latitude + "\nlongitude is "+ longitude + "\n";
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
firsttime = false;
}
}
mc.animateTo(p);
mc.setZoom((int) zoomamount);
mapView.invalidate();
setOverlay1(p);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
This sets Overlay push pin




public void setOverlay1(GeoPoint p) {
// Create itemizedOverlay
mapOverlays = mapView.getOverlays();
drawable1 = this.getResources().getDrawable(R.drawable.pushpin);
itemizedOverlay1 = new MyItemizedOverlay(drawable1);
OverlayItem rwwoverlay = new OverlayItem(p, “Isaac’s Restaurant", “Centerville Road");
itemizedOverlay1.addOverlay(rwwoverlay);
mapOverlays.add(itemizedOverlay1);
// Added symbols will be displayed when map is redrawn so force redraw
mapView.postInvalidate();
}
push pin Image
Put PushPin.png image in Drawable
push pin Image

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> myOverlays;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
myOverlays = new ArrayList<OverlayItem>();
populate();
}
public void addOverlay(OverlayItem overlay) {
myOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return myOverlays.get(i);
}
// Removes overlay item i
public void removeItem(int i) {
myOverlays.remove(i);
populate();
}
@Override
protected boolean onTap(int i) {// Handle tap events on overlay icons
GeoPoint gpoint = myOverlays.get(i).getPoint();
double lat = gpoint.getLatitudeE6() / 1e6;
double lon = gpoint.getLongitudeE6() / 1e6;
String toast = "Title: " + myOverlays.get(i).getTitle();
toast += "\nText: " + myOverlays.get(i).getSnippet();
toast += "\nSymbol coordinates: Lat = " + lat + " Lon = " + lon+ " (microdegrees)";
Toast.makeText(mContext, toast, Toast.LENGTH_LONG).show();
return (true);
}
@Override
public int size() {
return myOverlays.size();
}


On Tap pop up Toast with Info
Make Sure AndroidManifest.xml File is OK
 Add permissions for GPS
 To modify the AndroidManifest.xml file:
1.Click on the res folder in the GPSSimulator project.
2.Double-click AndroidManifest.xml to display the
GPSSimulator Manifest.
3.Enter the following lines before the application tag.

<uses-permission android:name=“android.permission.ACCESS_FINE_LOCATION”
/>
<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />


1.Save the changes to the file.
Add ability to use Google Maps
 Update the Manifest with two lines.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package=“EZSolution.android.GPSSimulator">

<uses-permission
android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />

<application android:icon="@drawable/icon"
android:label="@string/app_name">

<uses-library android:name="com.google.android.maps" />

<activity android:name=".GPS" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Add permissions
Need google map library
Test Configuration and Control

Control GPS
coordinates through
Eclipse

DDMS->Location
Controls -> send
button

enter coordinates
manually, or use
GPX or KML file

Or try it on a phone
Enter lat and long
DDMS
Map and Driving Directions

Need Locations of each store
(GPS latitude and longitude)

Need Location of phone (GPS)

Call Google Maps API with App

Directions Button
Sample Driving Directions Code
• public void DrawDrivingDirections() {
• String geoUriString;
• String srcLat;
• String srcLng;

double Issaclat = 40.05022000;// Issac's Centerville Road get via xml


double Issaclong = -76.39240300;// Issac's Centerville Road get via xml

• String dstLat = String.format("%f", Issaclat);
• String dstLng = String.format("%f", Issaclong);
• if (latitude > -1) {
• srcLat = String.format("%f", latitude);
• srcLng = String.format("%f", longitude);
• geoUriString = "http://maps.google.com/maps?" + "saddr="
• + srcLat + "," + srcLng + "&daddr=" + dstLat + "," + dstLng;
• }
• else//no GPS
• {
• geoUriString = "http://maps.google.com/maps?" + "saddr="+ "&daddr=" + dstLat + "," + dstLng;
• }
• Intent mapCall = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUriString));
• startActivity(mapCall);
• }
Need google map library
Notes
• @Override
• public void onDestroy() {

super.onDestroy();

• try {
turnOffGPSSearch();
• } catch (Exception e) {
•// TODO Auto-generated catch block
• }

}


public void turnOffGPSSearch() {
if (lm != null) {
lm.removeUpdates(locationListener);
lm = null;

}
if (locationListener != null) {
locationListener = null;
}
}

Make sure to kill GPS when leaving the Activity


LocationManager lm;
LocationListener locationListener;


lm = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);

locationListener = new MyLocationListener();

See demo code




Means GPS
Is still ON
Review

Use LocationManager to get Location

Set Map Overlay image at Location (pushpin.png)

Let’s Try it!!