Recently made a similar people near a micro-channel function like this, would like to get the latitude and longitude calculations, there is no problem on the 4.0, but in 2.3 it is not, seek the guidance of the great God.
package org.crazyit.gps;<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
import java.util.List;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class LocationTest extends Activity {
private LocationManager manager;
private SharedPreferences sp;
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
manager = (LocationManager) getSystemService (LOCATION_SERVICE) ;/ / get cell phone location information
Listproviders = manager.getAllProviders ();
/ / Conditions for access
Criteria criteria = new Criteria ();
criteria.setAccuracy (Criteria.ACCURACY_FINE) ;/ / obtain precise location
criteria.setCostAllowed (true) ;/ / allows the overhead
criteria.setPowerRequirement (Criteria.POWER_HIGH) ;/ / consume large, high frequency acquired
criteria.setSpeedRequired (true) ;/ / phone position
criteria.setAltitudeRequired (true) ;/ / altitude
/ / Get the best provider: gps
are on the phone or emulatorString bestProvider = manager.getBestProvider (criteria, true) ;/ / use GPS satellite
/ / System.out.println ("The best location provider is" + bestProvider);
sp = getSharedPreferences ("config", MODE_PRIVATE);
/ / Parameter:.. 1 provider 2 once every 3 How much time to get the number of meters every 4 listener trigger callback
..manager.requestLocationUpdates (bestProvider, 3000,100, new MyLocationListener ());
}
private class MyLocationListener implements LocationListener {
/ **
* Mobile location changes occur
* /
public void onLocationChanged (Location location) {
location.getAccuracy () ;/ / accuracy
String latitude = location.getLatitude () + "" ;/ / longitude
String longitude = location.getLongitude () + "" ;/ / latitude
System.out.println ("longitude" + latitude + "latitude" + longitude);
/ / To get to the latitude and longitude information into SharedPreferences
Editor editor = sp.edit ();
editor.putString ("lastloaction", latitude + "-" + longitude);
editor.commit ();
}
/ **
* When a location provider status changes
* /
public void onStatusChanged (String provider, int status, Bundle extras) {
}
/ **
* When you open a device
* /
public void onProviderEnabled (String provider) {
}
/ **
* A device is turned off
* /
public void onProviderDisabled (String provider) {
}
}
}
No comments:
Post a Comment