Listing 104 srccomcookbookupdateJocationMyLocationjava

package com.cookbook.update_location;

import android.app.Activity; import android.content.Context; import android.location.Criteria; import android.location.Location; import android.location.LocationListener;

import android.location.LocationManager; import android.os.Bundle; import android.widget.TextView;

public class MyLocation extends Activity implements LocationListener { LocationManager mLocationManager; TextView tv; Location mLocation;

^Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.tvl);

mLocationManager = (LocationManager)

getSystemService(Context.LOCATION_SERVICE);

Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setPowerRequirement(Criteria.POWER_LOW); String locationprovider =

mLocationManager.getBestProvider(criteria,true);

mLocation =

mLocationManager.getLastKnownLocation(locationprovider); mLocationManager.requestLocationUpdates(

locationprovider, 5000, 2.0, this);

@Override public void onLocationChanged(Location location) { mLocation = location; showupdate();

// these methods are required public void onProviderDisabled(String arg0) {}

public void onProviderEnabled(String provider) {}

public void onStatusChanged(String a, int b, Bundle c) {}

public void showupdate(){

tv.setText("Last location lat:"+mLocation.getLatitude() + " long:" + mLocation.getLongitude());

Note that rather than implementing the LocationListener at the activity level, it can also be declared as a separate inner class as follows.This can easily be added to any of the following recipes to provide an update mechanism to the location:

mLocationManager.requestLocationUpdates(

locationprovider, 5000, 2.0, myLocL);

private final LocationListener myLocL = new LocationListener(){ ^Override public void onLocationChanged(Location location){ mLocation = location; showupdate();

// these methods are required public void onProviderDisabled(String arg0) {}

public void onProviderEnabled(String provider) {}

public void onStatusChanged(String a, int b, Bundle c) {}

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook


Post a comment