Accessing GPS information on your Android Phone
( Modified from Original Source at [Link] !
"sing #clipse$ create a new Android pro%ect and name it &'S.%ava.
(o use &'S functionalit) in )our Android application$ )ou*ll need to add the
A++#SS,-./#,0O+A(.O/ permission to [Link] file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link]"
android:versionCode="1"
android:versionName="1.0">
<uses-permission
android:name="[Link].ACCESS_F!E_"#CA$#!" />
<application android:icon="%dra&a'(e/icon" android:label="%string/app_name">
<activity android:name=".androidgps"
android:label="%string/app_name">
<intent-filter>
<action android:name="[Link].)A!" />
<category android:name="[Link]*."A+!C,E-" />
</intent-filter>
</activity>
</application>
</manifest>
.n Android$ location12ased services are provided 2) the 0ocationManager class located in the
[Link] pac3age. "sing the 0ocationManager class$ )our application can o2tain periodic
updates of the device*s geographical locations as well as fire an intent when it enters the proximit) of a
certain location.
.n the &'S.%ava file$ first o2tain a reference to the 0ocationManager class using the getS)stemService(!
method. (o 2e notified whenever there is a change in location$ )ou need to register for a re4uest for
changes in locations so that )our program can 2e notified periodicall). (his is done via the
re4uest0ocation"pdates(!. (his method ta3es in four parameters:
provider: (he name of the provider with which )ou register
min(ime: (he minimum time interval for notifications$ in milliseconds.
min5istance: (he minimum distance interval for notifications$ in meters.
listener: An o2%ect whose on0ocation+hanged(! method will 2e called for each location update.
.n this example$ )ou*re more interested in what happens when a location changes$ so )ou*ll write some
codein the on0ocation+hanged(!method.
Specificall)$ when a location changes )ou will displa) a small dialog on the screen showing the new
location information: latitude and longitude. 6ou show this dialog using the (oast class. (he complete
&'S.%ava now loo3s li3e:
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class androidgps extends Activity {
!! Called w"en t"e activity is #irst created. !
private LocationManager lm;
private LocationListener locationListener;
!! Called w"en t"e activity is #irst created. !
$%verride
public void onCreate&Bundle saved'nstance(tate) {
[Link]&saved'nstance(tate);
setContent*iew&+.[Link]);
,,,use t"e LocationManager class to obtain -.( locations,,,
lm / &LocationManager)
get(ystem(ervice&Context.L%CAT'%01(2+*'C2);
locationListener / new MyLocationListener&);
lm.re3uestLocation4pdates&
LocationManager.-.(1.+%*'52+6
76
76
locationListener);
8
private class MyLocationListener implements LocationListener
{
$%verride
public void onLocationC"anged&Location loc) {
i# &loc 9/ null) {
[Link]&getBaseContext&)6
:Location c"anged ; Lat; : < [Link]&) <
: Lng; : < [Link]&)6
Toast.L20-T=1(=%+T).s"ow&);
8
8
$%verride
public void on.rovider5isabled&(tring provider) {
T%5% Auto,generated met"od stub
8
$%verride
public void on.rovider2nabled&(tring provider) {
T%5% Auto,generated met"od stub
8
$%verride
public void on(tatusC"anged&(tring provider6 int status6
Bundle extras) {
T%5% Auto,generated met"od stub
888
Using GPS with Google Maps
(Modified from original source at: [Link]
[Link] ,
%impl* displa*ing the latitude and longitude when a location has changed is not ver* interesting. A much more
interesting thing to do would e to couple the data together with the +oogle Maps application.
-hen *ou create *our application choose the +oogle A'( (as the target device,. (f not listed. *ou need to update
/clipse using the avd update manager (under windows menu item in eclipse,.
As *ou learnt in the previous %ection. for +oogle Maps to wor0. *ou need to add the CC!""#$%N!#&'C(%'N
permission into the [Link] file and then use the +oogle Maps lirar*.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
package="[Link](e">
<uses-permission
android:name="[Link].!$E-!E$" />
<uses-permission
android:name="[Link].ACCESS_F!E_"#CA$#!" />
<application android:icon="%dra&a'(e/icon"
android:label="%string/app_name">
<uses-library android:name="[Link]([Link]" />
<activity android:name="../S_goog(e" android:label="%string/app_name">
<intent-filter>
<action android:name="[Link].)A!" />
<category android:name="[Link]*."A+!C,E-" />
</intent-filter>
</activity>
</application>
</manifest>
(n main)xml. replace the <(ext*ie+> element with the <,ap*ie+> element:
<?xml version="1.0" encoding="utf-8"?>
<&inear&ayout xmlns:android="[Link]
android:orientation="0ertica("
android:layout#+idt-="fi((_parent"
android:layout#-eig-t="fi((_parent"
>
<com)google)android)maps),ap*ie+
android:id="%1id/map0ie&1"
android:layout#+idt-="fi((_parent"
android:layout#-eig-t="fi((_parent"
android:enabled="true"
android:clickable="true"
android:[Link]="apike*" />
</&inear&ayout>
(he google maps api uses an api3e) (7api3e)8 value in [Link] file needs to 2e replaced with this!.
(his is li3e a code signing 3e) specific to the computer )ou are developing on. (he steps to generate
the api3e) are:
1 0ocate the de2ug.3e)store file on )our computer. On windows 9' this is located in:
+::5ocuments and Settings:()our account name!:.android
1 +op) the de2ug.3e)store file to the android tools folder in the location for )our S5; on the machine.
1 -rom the tools folder run the command :
keytool ,list ,alias androiddebugkey ,storepass android ,keypass android ,keystore [Link]
1 Once )ou hit the enter 3e)$ )ou*ll see something li3e this (the actual M5< that we*re interested in is
the last line!:
androiddebugkey6 Mar >76 ?77@6 .rivateAey2ntry6
Certi#icate #ingerprint &M5B); 5>;>C;DA;B5;EF;EF;AD;BC;@5;C5;@A;DD;A?;CC;>>;AC
1 /ow open )our 2rowser and go to the following url...
[Link]
1 Accept the agreement and paste the md< that the 3e) tool returned to )ou$ in m) case it was
something li3e...
5=:=>:?A:@[Link]?:<>:95:+[Link]??:A:>+:==:A+
(hen press B&enerate A'. ;e)B and )ou*ll 2e redirected to a page that has )our api 3e).
/ow that )ou have the api 3e)$ )ou can use it in the [Link] la)out file of )our Map(est application.
Cere*s what mine loo3s li3e:
<?xml version="1.0" encoding="utf-8"?>
<&inear&ayout xmlns:android="[Link]
android:orientation="0ertica("
android:layout#+idt-="fi((_parent"
android:layout#-eig-t="fi((_parent"
>
<com)google)android)maps),ap*ie+
android:id="%1id/map0ie&1"
android:layout#+idt-="fi((_parent"
android:layout#-eig-t="fi((_parent"
android:enabled="true"
android:clickable="true"
android:[Link]="0d2-3Er#-p/E4t53hh86FF7s0ka-A8u8k9An:&" />
</&inear&ayout>
Finally, modify the .4a0a file in your project to incorporate Google Maps.
package [Link](e<
import [Link]([Link]..eo/oint<
import [Link]([Link].)apActi0it*<
import [Link]([Link].)apContro((er<
import [Link]([Link].)ap=ie&<
import [Link].Conte3t<
import android.(ocation."ocation<
import android.(ocation."ocation"istener<
import android.(ocation."ocation)anager<
import [Link].7und(e<
import android.&idget.$oast<
public class ./S_goog(e extends )apActi0it*
>
private "ocation)anager (m<
private "ocation"istener (ocation"istener<
private )ap=ie& map=ie&<
private )apContro((er mc<
/?? Ca((ed &hen the acti0it* is first created. ?/
%#0erride
public void onCreate@7und(e sa0ednstanceStateA >
[Link]@sa0ednstanceStateA<
setContent=ie&@-.(a*[Link]<
//---use the "ocation)anager c(ass to o'tain ./S (ocations---
(m B @"ocation)anagerA
getS*stemSer0ice@Conte3t."#CA$#!_SE-=CEA<
(ocation"istener B new )*"ocation"istener@A<
([Link]"ocation+pdates@
"ocation)anager../S_/-#=DE-E
0E
0E
(ocation"istenerA<
map=ie& B @)ap=ie&A find=ie&7*d@-.id.map0ie&1A<
mc B map=ie&.getContro((er@A<
F
%#0erride
protected boolean is-outeDisp(a*ed@A >
// TODO Auto-generated method stu'
return false<
F
private class )*"ocation"istener implements "ocation"istener
>
%#0erride
public void on"ocationChanged@"ocation (ocA >
if @(oc GB nullA >
$[Link]$e3t@get7aseConte3t@AE
""ocation changed : "at: " 1 ([Link]"atitude@A 1
" "ng: " 1 ([Link]"ongitude@AE
$oast."E!.$,_S,#-$[Link]&@A<
.eo/oint p B new .eo/oint@
@intA @([Link]"atitude@A ? 1EHAE
@intA @([Link]"ongitude@A ? 1EHAA<
[Link]$o@pA<
mc.set5oom@1HA<
map=ie&.in0a(idate@A<
F
F
%#0erride
public void on/ro0iderDisa'(ed@String pro0iderA >
// TODO Auto-generated method stu'
F
%#0erride
public void on/ro0iderEna'(ed@String pro0iderA >
// TODO Auto-generated method stu'
F
%#0erride
public void onStatusChanged@String pro0iderE int statusE
7und(e e3trasA >
// TODO Auto-generated method stu'
F
F
F
(n the aove. when a location changes. the latitude and longitude is sent to the +oogle Maps application. which
then displa*s the map of the current location
Accessing Accelerometer information on your Android Phone
(Modified from original source at: [Link]
[Link] !
An instance of the SensorManager is re4uired in order to retrieve informations a2out the supported
sensors. /o permission is re4uired to access the sensor service. .t is then possi2le to retrieve the list of
availa2le sensors of a certain t)pe. -or an accelerometer sensor$ the t)pe to use is given 2) the
Sensor.(6'#,A++#0#FOM#(#F constant. .f at least one Sensor exists$ it is possi2le to register a
Sensor#vent0istener for a Sensor of the list. .t is possi2le to specif) the delivering rate for sensor
events. Specified rate must 2e one of :
SensorManager.S#/SOF,5#0A6,-AS(#S( : as fast as possi2le
SensorManager.S#/SOF,5#0A6,&AM# : rate suita2le for game
SensorManager.S#/SOF,5#0A6,/OFMA0 : normal rate
SensorManager.S#/SOF,5#0A6,". : rate suita2le for ". (hread
6ou need to first specif) all the varia2les in the [Link] file which form the &". of )our
accelerometer application:
<?xml version="1.0" encoding="utf-8"?>
<(able&ayout xmlns:android="[Link]
android:layout#+idt-="fi((_parent"
android:layout#-eig-t="fi((_parent"
android:stretc-Columns="1">
<(able/o+>
<(ext*ie+
android:id="%1id/acce(erometer_(a'e("
android:layout#column="1"
android:text="Acce(erometer"
android:text"i0e="9pt"
android:padding="Idip" />
</(able/o+>
<(able/o+>
<(ext*ie+
android:id="%1id/acce(_3_(a'e("
android:layout#column="1"
android:text="8:"
android:text"i0e="8pt"
android:padding="Idip" />
<(ext*ie+
android:id="%1id/acce(_3_0a(ue"
android:gravity="right"
android:text"i0e="8pt"
android:padding="Idip" />
</(able/o+>
<(able/o+>
<(ext*ie+
android:id="%1id/acce(_*_(a'e("
android:layout#column="1"
android:text="J:"
android:text"i0e="8pt"
android:padding="Idip" />
<(ext*ie+
android:id="%1id/acce(_*_0a(ue"
android:gravity="right"
android:text"i0e="8pt"
android:padding="Idip" />
</(able/o+>
<(able/o+>
<(ext*ie+
android:id="%1id/acce(_:_(a'e("
android:layout#column="1"
android:text="5:"
android:text"i0e="8pt"
android:padding="Idip" />
<(ext*ie+
android:id="%1id/acce(_:_0a(ue"
android:gravity="right"
android:text"i0e="8pt"
android:padding="Idip" />
</(able/o+>
<*ie+
android:layout#-eig-t="6dip"
android:background="KFF909090" />
<(able/o+>
<(ext*ie+
android:id="%1id/orientation_(a'e("
android:layout#column="1"
android:text="#rientation"
android:text"i0e="9pt"
android:padding="Idip" />
</(able/o+>
<(able/o+>
<(ext*ie+
android:id="%1id/orient_3_(a'e("
android:layout#column="1"
android:text="8:"
android:text"i0e="8pt"
android:padding="Idip" />
<(ext*ie+
android:id="%1id/orient_3_0a(ue"
android:gravity="right"
android:text"i0e="8pt"
android:padding="Idip" />
</(able/o+>
<(able/o+>
<(ext*ie+
android:id="%1id/orient_*_(a'e("
android:layout#column="1"
android:text="J:"
android:text"i0e="8pt"
android:padding="Idip" />
<(ext*ie+
android:id="%1id/orient_*_0a(ue"
android:gravity="right"
android:text"i0e="8pt"
android:padding="Idip" />
</(able/o+>
<(able/o+>
<(ext*ie+
android:id="%1id/orient_:_(a'e("
android:layout#column="1"
android:text="5:"
android:text"i0e="8pt"
android:padding="Idip" />
<(ext*ie+
android:id="%1id/orient_:_0a(ue"
android:gravity="right"
android:text"i0e="8pt"
android:padding="Idip" />
</(able/o+>
</(able&ayout>
.n the case of a Sensor#vent triggered 2) a Sensor of t)pe Sensor. (6'#,A++#0#FOM#(#F$ the
eventGs values represents the acceleration of the phone given 2) a vector in a cartesian coordinate
s)stem. 0anding on a ta2le$ the values returned 2) the Sensor#vent for the phone should 2e :
=. E m/s along x axis
. E m/s along ) axis
3. 9$DE>>< m/s along H axis
-rom an event to another$ the coordinates of the acceleration vector are stored to detect sudden
acceleration changes and to trigger a sha3e event when the threshold is reached. Others events could 2e
implemented such as the detection of up and down gestures$ circular gestures and lot more.
As usual$ it is prefera2le to register listeners in the onFesume(! method of the Activit) and removed in
the on-inish(! method of the Activit).
(hen add the following code to )our activit) class 2efore compiling on )our android device (note that
accelerometer code will onl) wor3 for a device and not the emulator!.
package [Link];
import [Link];
import android."ardware.(ensor;
import android."ardware.(ensor2vent;
import android."ardware.(ensor2ventListener;
import android."ardware.(ensorManager;
import [Link];
import [Link]*iew;
public class accelerometer extends Activity implements (ensor2ventListener {
Accelerometer G6 H6 and I values
private Text*iew accelG*alue;
private Text*iew accelH*alue;
private Text*iew accelI*alue;
%rientation G6 H6 and I values
private Text*iew orientG*alue;
private Text*iew orientH*alue;
private Text*iew orientI*alue;
private (ensorManager sensorManager / null;
!! Called w"en t"e activity is #irst created. !
$%verride
public void onCreate&Bundle saved'nstance(tate) {
[Link]&saved'nstance(tate);
-et a re#erence to a (ensorManager
sensorManager / &(ensorManager) get(ystem(ervice&(20(%+1(2+*'C2);
setContent*iew&+.[Link]);
Capture accelerometer related view elements
accelG*alue / &Text*iew) #ind*iewBy'd&+.id.accel1x1value);
accelH*alue / &Text*iew) #ind*iewBy'd&+.id.accel1y1value);
accelI*alue / &Text*iew) #ind*iewBy'd&+.id.accel1J1value);
Capture orientation related view elements
orientG*alue / &Text*iew) #ind*iewBy'd&+.id.orient1x1value);
orientH*alue / &Text*iew) #ind*iewBy'd&+.id.orient1y1value);
orientI*alue / &Text*iew) #ind*iewBy'd&+.id.orient1J1value);
'nitialiJe accelerometer related view elements
accelG*[Link]&:7.77:);
accelH*[Link]&:7.77:);
accelI*[Link]&:7.77:);
'nitialiJe orientation related view elements
orientG*[Link]&:7.77:);
orientH*[Link]&:7.77:);
orientI*[Link]&:7.77:);
8
T"is met"od will update t"e 4' on new sensor events
public void on(ensorC"anged&(ensor2vent sensor2vent) {
synchronized &this) {
if &[Link]&) // ([Link].21ACC2L2+%M2T2+) {
accelG*[Link]&[Link](tring&sensor2vent.valuesL7M));
accelH*[Link]&[Link](tring&[Link]>M));
accelI*[Link]&[Link](tring&[Link]?M));
8
if &[Link]&) // ([Link].21%+'20TAT'%0) {
orientG*[Link]&[Link](tring&sensor2vent.valuesL7M));
orientH*[Link]&[Link](tring&[Link]>M));
orientI*[Link]&[Link](tring&[Link]?M));
8
8
8
'Nve c"osen to not implement t"is met"od
public void onAccuracyC"anged&(ensor arg76 int arg>) {
TODO Auto,generated met"od stub
8
$%verride
protected void on+esume&) {
[Link]+esume&);
+egister t"is class as a listener #or t"e accelerometer sensor
[Link]&this6 sensorManager.get5e#ault(ensor&([Link].21ACC2L2+%M2T2+)6
(ensorManager.(20(%+152LAH10%+MAL);
...and t"e orientation sensor
[Link]&this6 sensorManager.get5e#ault(ensor&([Link].21%+'20TAT'%0)6
(ensorManager.(20(%+152LAH10%+MAL);
8
$%verride
protected void on(top&) {
4nregister t"e listener
[Link]&this);
[Link](top&);
8
8