O'Reilly logo

Programming Android by Zigurd Mednieks, G. Blake Meike, Masumi Nakamura, Laird Dornin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Location Without Maps

What if your activity needs to access location information, but it doesn’t include a MapView? When you use a MapView, Android makes everything very easy with MyLocationOverlay, but if you don’t need a map it still isn’t that hard to get location information. The code in this section is not part of MJAndroid, but it shows how you obtain location information independent of MapView.

Let’s look at a very simple, one-activity application that displays the current location in a TextView.

The Manifest and Layout Files

An appropriate AndroidManifest.xml file follows. We created this file using the Android SDK and the Android Manifest Editor that comes as part of it. The only change we needed to make with the editor was to add the uses-permission tag for android.permission.ACCESS_FINE_LOCATION (in the next-to-last line of the file). We always need this permission in order to get location information from a GPS location provider:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.microjobsinc.dloc" android:versionCode="1" android:versionName="1.0.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Main" 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> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required