Android Programming Tutorials

(Romina) #1
Posts On Location

android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application android:label="@string/app_name">
<activity android:name=".Patchy"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".EditPreferences">
</activity>
<service android:name=".PostMonitor" />
</application>
</manifest>

Then, add some imports to Patchy for location-related classes that we will


need:


import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

Next, add a LocationManager data member named locMgr in Patchy, then


initialize it in onCreate():


locMgr=(LocationManager)getSystemService(LOCATION_SERVICE);

At this point, we can start using the LocationManager for getting the location


to embed in the status update.


Step #2: Register for Location Updates


Next, we need to do something to cause Android to actually activate GPS


and get fixes. Just having access to LocationManager is insufficient. The


simplest answer is to register for location updates, even if we will use


another way to get our current fix, as you will see later in this tutorial.


So, add the following statement to onCreate() in Patchy, after you have


initialized locMgr as shown in the preceding section:


228
Free download pdf