Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
59 views

Accessing GPS Information On Your Android Phone

This document provides instructions for accessing GPS information on an Android phone and displaying GPS location data on a Google Maps view. It describes adding the ACCESS_FINE_LOCATION permission, obtaining location updates from the LocationManager class, and registering a LocationListener. It then explains how to generate an API key for Google Maps and display the user's coordinates on a map when the location changes.

Uploaded by

Jose
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Accessing GPS Information On Your Android Phone

This document provides instructions for accessing GPS information on an Android phone and displaying GPS location data on a Google Maps view. It describes adding the ACCESS_FINE_LOCATION permission, obtaining location updates from the LocationManager class, and registering a LocationListener. It then explains how to generate an API key for Google Maps and display the user's coordinates on a map when the location changes.

Uploaded by

Jose
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Accessing GPS information on your Android Phone

( Modified from Original Source at http://www.devx.com/wireless/Article/3939 !


"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 theAndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.gps"
android:versionCode="1"
android:versionName="1.0">
<uses-permission
android:name="android.permission.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="android.intent.action.)A!" />
<category android:name="android.intent.categor*."A+!C,E-" />
</intent-filter>
</activity>
</application>
</manifest>
.n Android$ location12ased services are provided 2) the 0ocationManager class located in the
android.location 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 android.gps;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.Toast;
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) {
super.onCreate&saved'nstance(tate);
setContent*iew&+.layout.main);

,,,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) {
Toast.makeText&getBaseContext&)6
:Location c"anged ; Lat; : < loc.getLatitude&) <
: Lng; : < loc.getLongitude&)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: http://www.devx.com/wireless/Article/39239/0/page/3
http://www.remwedevelopment.com/dev/a3!/Android"#ow"$o"%et"&p"an"A'(")e*"for"+oogle"Maps.html ,
%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 Androidmanifest.xml file and then use the +oogle Maps lirar*.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gps.goog(e">

<uses-permission
android:name="android.permission.!$E-!E$" />
<uses-permission
android:name="android.permission.ACCESS_F!E_"#CA$#!" />

<application android:icon="%dra&a'(e/icon"
android:label="%string/app_name">

<uses-library android:name="com.goog(e.android.maps" />

<activity android:name="../S_goog(e" android:label="%string/app_name">
<intent-filter>
<action android:name="android.intent.action.)A!" />
<category android:name="android.intent.categor*."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="http://schemas.android.com/apk/res/android"
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:api.ey="apike*" />

</&inear&ayout>
(he google maps api uses an api3e) (7api3e)8 value in main.xml 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 debug.keystore
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...
http://code.google.com/intl/%a/android/maps1api1signup.html
1 Accept the agreement and paste the md< that the 3e) tool returned to )ou$ in m) case it was
something li3e...
5=:=>:?A:@5:A3:A3:A?:<>:95:+5:9A:??: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 main.xml 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="http://schemas.android.com/apk/res/android"
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:api.ey="0d2-3Er#-p/E4t53hh86FF7s0ka-A8u8k9An:&" />

</&inear&ayout>
Finally, modify the .4a0a file in your project to incorporate Google Maps.
package gps.goog(e<
import com.goog(e.android.maps..eo/oint<
import com.goog(e.android.maps.)apActi0it*<
import com.goog(e.android.maps.)apContro((er<
import com.goog(e.android.maps.)ap=ie&<
import android.content.Conte3t<
import android.(ocation."ocation<
import android.(ocation."ocation"istener<
import android.(ocation."ocation)anager<
import android.os.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 >
super.onCreate@sa0ednstanceStateA<
setContent=ie&@-.(a*out.mainA<

//---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<

(m.reCuest"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 >
$oast.make$e3t@get7aseConte3t@AE
""ocation changed : "at: " 1 (oc.get"atitude@A 1
" "ng: " 1 (oc.get"ongitude@AE
$oast."E!.$,_S,#-$A.sho&@A<

.eo/oint p B new .eo/oint@
@intA @(oc.get"atitude@A ? 1EHAE
@intA @(oc.get"ongitude@A ? 1EHAA<
mc.animate$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: http://2log.androgames.net/D</android1accelerometer1tutorial/
http://mo2ilestrategist.2logspot.com/E=E/E=/android1accelerometer1and1orientation.html !
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 main.xml file which form the &". of )our
accelerometer application:
<?xml version="1.0" encoding="utf-8"?>
<(able&ayout xmlns:android="http://schemas.android.com/apk/res/android"
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 android.accelerometer;
import android.app.Activity;
import android."ardware.(ensor;
import android."ardware.(ensor2vent;
import android."ardware.(ensor2ventListener;
import android."ardware.(ensorManager;
import android.os.Bundle;
import android.widget.Text*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) {
super.onCreate&saved'nstance(tate);
-et a re#erence to a (ensorManager
sensorManager / &(ensorManager) get(ystem(ervice&(20(%+1(2+*'C2);
setContent*iew&+.layout.main);

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*alue.setText&:7.77:);
accelH*alue.setText&:7.77:);
accelI*alue.setText&:7.77:);

'nitialiJe orientation related view elements
orientG*alue.setText&:7.77:);
orientH*alue.setText&:7.77:);
orientI*alue.setText&: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 &sensor2vent.sensor.getType&) // (ensor.TH.21ACC2L2+%M2T2+) {
accelG*alue.setText&Kloat.to(tring&sensor2vent.valuesL7M));
accelH*alue.setText&Kloat.to(tring&sensor2vent.valuesL>M));
accelI*alue.setText&Kloat.to(tring&sensor2vent.valuesL?M));
8

if &sensor2vent.sensor.getType&) // (ensor.TH.21%+'20TAT'%0) {
orientG*alue.setText&Kloat.to(tring&sensor2vent.valuesL7M));
orientH*alue.setText&Kloat.to(tring&sensor2vent.valuesL>M));
orientI*alue.setText&Kloat.to(tring&sensor2vent.valuesL?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&) {
super.on+esume&);
+egister t"is class as a listener #or t"e accelerometer sensor
sensorManager.registerListener&this6 sensorManager.get5e#ault(ensor&(ensor.TH.21ACC2L2+%M2T2+)6
(ensorManager.(20(%+152LAH10%+MAL);
...and t"e orientation sensor
sensorManager.registerListener&this6 sensorManager.get5e#ault(ensor&(ensor.TH.21%+'20TAT'%0)6
(ensorManager.(20(%+152LAH10%+MAL);
8

$%verride
protected void on(top&) {
4nregister t"e listener
sensorManager.unregisterListener&this);
super.on(top&);
8

8

You might also like