Listing 105 srccomcookbookshowprovidersMyLocationjava

package com.cookbook.show_providers;

import java.util.List;

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 { LocationManager mLocationManager; TextView tv;

Location mLocation; ^Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.tv1); 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);

List<String> providers = mLocationManager.getProviders(true); StringBuilder mSB = new StringBuilder("Providers:\n"); for(int i = 0; i<providers.size(); i++) { mLocationManager.requestLocationUpdates(

providers.get(i), 5000, 2.0f, new LocationListener(){

// these methods are required public void onLocationChanged(Location location) {} public void onProviderDisabled(String arg0) {} public void onProviderEnabled(String provider) {}

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

mSB.append(providers.get(i)).append(": \n"); mLocation =

mLocationManager.getLastKnownLocation(providers.get(i)); if(mLocation != null) {

mSB.append(mLocation.getLatitude()).append(" , "); mSB.append(mLocation.getLongitude()).append("\n"); } else {

mSB.append("Location can not be found");

tv.setText(mSB.toString());

mylocation

Providers:2 network:

37.389169, -121.905499 gps:

37.3880741 21.89528490000001

Figure 10.1 Example output of all enabled location providers at their lastKnownLocation using an actual Android device.

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