The LocationManager lm and the LocationListener locationListener are declared in the Activity at lines 16-17
The locationListener is linked to the GPS_PROVIDER and the NETWORK_PROVIDER with a period and a range of movement. lines 27 to 37.
onLocationChanged calls the private function getGPS().
getGPS() gets a list of the location providers, and then loops through them displaying some information about them.
Using java Syntax Highlighting
- package com.example.location;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.util.Log;
- public class LocationServices extends Activity {
- LocationManager lm;
- private LocationListener locationListener;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
- locationListener = new MyLocationListener();
- lm.requestLocationUpdates(
- LocationManager.GPS_PROVIDER,
- 3000, // ms since last call
- 0, // m movement
- locationListener);
- lm.requestLocationUpdates(
- LocationManager.NETWORK_PROVIDER,
- 200, // ms since last call
- 0, // m movement
- locationListener);
- }
- public class MyLocationListener implements LocationListener
- {
- public void onLocationChanged(Location loc) {
- double coordinates[] = getGPS();
- }
- public void onProviderDisabled(String provider) {
- // TODO Auto-generated method stub
- }
- public void onProviderEnabled(String provider) {
- // TODO Auto-generated method stub
- Log.d("Hello7","onProviderEnabled");
- }
- public void onStatusChanged(String provider, int status, Bundle extras) {
- }
- }
- onLocationChanged calls getGPS()
- private double[] getGPS() {
- List<String> providers = lm.getProviders(true);
- double[] gps = new double[2];
- /* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
- Location l = null;
- for (int i=providers.size()-1; i>=0; i--) {
- String s = providers.get(i);
- Log.d("LocServ",String.format("provider (%d) is %s",i,s));
- l = lm.getLastKnownLocation(providers.get(i));
- if (l != null) {
- gps[0] = l.getLatitude();
- gps[1] = l.getLongitude();
- Log.d("LocServ",String.format("Lat %f, Long %f accuracy=%f",gps[0],gps[1],l.getAccuracy()));
- }
- }
- return gps;
- }
- }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.location"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".LocationServices"
- 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-sdk android:minSdkVersion="3" />
- </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4

