Android 学习笔记-2010年10月

10-14

1、导航例子:

import java.io.IOException; import java.util.List; import java.util.Locale; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; import android.content.Context; import android.location.Address; import android.location.Criteria; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class WhereAmI extends MapActivity { /** Called when the activity is first created. */ public MapController mapController; public MyPositionOverlay myPosition; public MapView myMapView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LocationManager locationManager; String context=Context.LOCATION_SERVICE; locationManager=(LocationManager)getSystemService(context); myMapView=(MapView)findViewById(R.id.myMapView); mapController=myMapView.getController(); myMapView.setSatellite(true); myMapView.setStreetView(true); myMapView.displayZoomControls(false); mapController.setZoom(17); myPosition=new MyPositionOverlay(); ListOverlay> overlays=myMapView.getOverlays(); overlays.add(myPosition); Criteria criteria =new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(false); criteria.setPowerRequirement(Criteria.POWER_LOW); String provider=locationManager.getBestProvider(criteria, true); Location location=locationManager.getLastKnownLocation(provider); updateWithNewLocation(location); locationManager.requestLocationUpdates(provider, 3000, 0, locationListener); } //TextView text; private void updateWithNewLocation(Location location) { // TODO Auto-generated method stub String latLongString; TextView myLocationText = (TextView)findViewById(R.id.myLocationText); String addressString="no address found/n"; if(location!=null){ myPosition.setLocation(location); Double geoLat=location.getLatitude()*1E6; Double geoLng=location.getLongitude()*1E6; GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue()); mapController.animateTo(point); double lat=location.getLatitude(); double lng=location.getLongitude(); latLongString="Lat :"+lat+"/nLong :"+lng; double latitude=location.getLatitude(); double longitude=location.getLongitude(); Geocoder gc=new Geocoder(this,Locale.getDefault()); try{ ListAddress> addresses=gc.getFromLocation(latitude, longitude, 1); StringBuilder sb=new StringBuilder(); if(addresses.size()>0){ Address address=addresses.get(0); for(int i=0;iaddress.getMaxAddressLineIndex();i++) sb.append(address.getAddressLine(i)).append("/n"); sb.append(address.getLocality()).append("/n"); sb.append(address.getPostalCode()).append("/n"); sb.append(address.getCountryName()); addressString=sb.toString(); } }catch(IOException e){} }else{ latLongString="Location not found./n"; } myLocationText.setText("Your current location is:/n"+latLongString+ "/n"+addressString); } private final LocationListener locationListener=new LocationListener(){ public void onLocationChanged(Location location) {updateWithNewLocation(location);} public void onProviderDisabled(String provider) {updateWithNewLocation(null);} public void onProviderEnabled(String provider){} public void onStatusChanged(String provider,int status,Bundle extras){} }; protected boolean isRouteDisplayed(){ return false; } private static final int ZOOM_IN=Menu.FIRST; private static final int ZOOM_OUT=Menu.FIRST+1; public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); menu.add(0, ZOOM_IN, Menu.NONE, "Zoom In"); menu.add(0, ZOOM_OUT, Menu.NONE, "Zoom Out"); return true; } public boolean onOptionsItemSelected(MenuItem item){ super.onOptionsItemSelected(item); switch(item.getItemId()){ case (ZOOM_IN): mapController.zoomIn(); return true; case (ZOOM_OUT): mapController.zoomOut(); return true; } return true; } } 

2、关于android gps、获取位置信息的好帖子

http://www.javaeye.com/topic/363950

 

 

10-15

1、创建一个背景模糊的Window,且将对话窗口放在前景的小技巧,

可以在自己的应用中尝试,带给用户不同的体验

final Dialog d = new Dialog(this); d.setTitle("something"); d.setContentView(R.layout.mydialog); /* 创建一个背景模糊的Window,且将对话窗口放在前景 */ Window window = d.getWindow(); window.setFlags ( WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND ); 

2、gps获得地理位置信息

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 CurrentLocation extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LocationManager locationManager; String serviceName = Context.LOCATION_SERVICE; locationManager = (LocationManager)getSystemService(serviceName); //String provider = LocationManager.GPS_PROVIDER; Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); String provider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(provider); updateWithNewLocation(location); locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); } private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateWithNewLocation(location); } public void onProviderDisabled(String provider){ updateWithNewLocation(null); } public void onProviderEnabled(String provider){ } public void onStatusChanged(String provider, int status, Bundle extras){ } }; private void updateWithNewLocation(Location location) { String latLongString; TextView myLocationText; myLocationText = (TextView)findViewById(R.id.myLocationText); if (location != null) { double lat = location.getLatitude(); double lng = location.getLongitude(); latLongString = "纬度:" + lat + "/n经度:" + lng; } else { latLongString = "无法获取地理信息"; } myLocationText.setText("您当前的位置是:/n" + latLongString); } } 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 CurrentLocation extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LocationManager locationManager; String serviceName = Context.LOCATION_SERVICE; locationManager = (LocationManager)getSystemService(serviceName); //String provider = LocationManager.GPS_PROVIDER; Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); String provider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(provider); updateWithNewLocation(location); locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); } private final LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { updateWithNewLocation(location); } public void onProviderDisabled(String provider){ updateWithNewLocation(null); } public void onProviderEnabled(String provider){ } public void onStatusChanged(String provider, int status, Bundle extras){ } }; private void updateWithNewLocation(Location location) { String latLongString; TextView myLocationText; myLocationText = (TextView)findViewById(R.id.myLocationText); if (location != null) { double lat = location.getLatitude(); double lng = location.getLongitude(); latLongString = "纬度:" + lat + "/n经度:" + lng; } else { latLongString = "无法获取地理信息"; } myLocationText.setText("您当前的位置是:/n" + latLongString); } } 

 

你可能感兴趣的:(android,String,service,null,import,2010)