1.
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress();
2.
try { Socket socket = new Socket("www.droidnova.com", 80); Log.i("", socket.getLocalAddress().toString()); } catch (Exception e) { Log.i("", e.getMessage()); }
public String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e(LOG_TAG, ex.toString()); } return null; }
3.
WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE); DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); String[] dhcpInfos = dhcpInfo.toString().split(" "); String realIP = "oops, no ip"; int i = 0; while (i < dhcpInfos.length) { if (dhcpInfos[i].equals("ipaddr")) { realIP = dhcpInfos[i+1]; break; } i++; }
4.
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
// [...]
tv = (TextView) findViewById(R.id.id_wlan_ip_address);
tv.setText(intToIp(dhcpInfo.ipAddress));
// [...]
/**
* http://teneo.wordpress.com/2008/12/23/java-ip-address-to-integer-and-back/
*/
private String intToIp(int i) {
return ( i & 0xFF) + "." +
((i >> 8 ) & 0xFF) + "." +
((i >> 16 ) & 0xFF) + "." +
((i >> 24 ) & 0xFF)
;
}
public class HelloGoogleMaps extends MapActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MapView mapView = (MapView) findViewById(R.id.mapview); mapView.setBuiltInZoomControls(true); List<Overlay> mapOverlays = mapView.getOverlays(); Drawable drawable = this.getResources().getDrawable(R.drawable.icon); HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this); GeoPoint point = new GeoPoint(30443769,-91158458); OverlayItem overlayitem = new OverlayItem(point, "Laissez les bon temps rouler!", "I'm in Louisiana!"); GeoPoint point2 = new GeoPoint(17385812,78480667); OverlayItem overlayitem2 = new OverlayItem(point2, "Namashkaar!", "I'm in Hyderabad, India!"); itemizedoverlay.addOverlay(overlayitem); itemizedoverlay.addOverlay(overlayitem2); mapOverlays.add(itemizedoverlay); } @Override protected boolean isRouteDisplayed() { return false; } }
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> { private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); private Context mContext; public HelloItemizedOverlay(Drawable defaultMarker, Context context) { super(boundCenterBottom(defaultMarker)); mContext = context; } public void addOverlay(OverlayItem overlay) { mOverlays.add(overlay); populate(); } @Override protected OverlayItem createItem(int i) { return mOverlays.get(i); } @Override public int size() { return mOverlays.size(); } @Override protected boolean onTap(int index) { OverlayItem item = mOverlays.get(index); AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); dialog.setTitle(item.getTitle()); dialog.setMessage(item.getSnippet()); dialog.show(); return true; } }
zhu:
GeoPoint[] mapPoints = new GeoPoint[mapPlaces.length]; OverlayItem[] mapItems = new OverlayItem[mapPlaces.length]; for(int i=0; i<mapPlaces.length;i++){ double latitude = Double.parseDouble(mapPlaces[i].getLatitude()); double longitude = Double.parseDouble(mapPlaces[i].getLongitude()); mapPoints[i] = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6)); mapItems[i] = new OverlayItem(mapPoints[i], mapPlaces[i].getTitle(), mapPlaces[i].getAddress()); itemizedoverlay.addOverlay(mapItems[i]); } mapOverlays.add(itemizedoverlay);
其他实现:
public class CenterOverlay extends ItemizedOverlay<OverlayItem> implements LocationListener { private static final String TAG = "CenterOverlay: "; private LocationManager mLocationManager; private long updateTime = 60000; private static final int updateDistance = 50; private GeoPoint lastKnownPoint; private Location lastKnownLocation; private Drawable centerDrawable; private Context mContext; private final List<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); private Paint accuracyPaint; private Point center; private Point left; private Drawable drawable; private int width; private int height; // San Francisco private final static double[] DEFAULT_LOCATION = { 37.7749295, -122.4194155 }; private Runnable firstFixRunnable = null; private boolean firstFixRun = false; public CenterOverlay(Drawable defaultMarker, MapView mapView, Context c) { super(boundCenter(defaultMarker)); this.centerDrawable = defaultMarker; this.mContext = c; mLocationManager = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE); if (Constants.DEBUG) { updateTime = 0; } else { updateTime = 60000; } } public void addOverlay(OverlayItem overlay) { mOverlays.add(overlay); populate(); } private void checkFirstRunnable() { if (!firstFixRun && lastKnownLocation != null && firstFixRunnable != null) { firstFixRunnable.run(); } } private OverlayItem createCenterOverlay(GeoPoint point) { OverlayItem i = new OverlayItem(point, "Location", null); i.setMarker(centerDrawable); return i; } private GeoPoint createGeoPoint(Location loc) { int lat = (int) (loc.getLatitude() * 1E6); int lng = (int) (loc.getLongitude() * 1E6); return new GeoPoint(lat, lng); } @Override protected OverlayItem createItem(int i) { return mOverlays.get(i); } public void disableLocation() { mLocationManager.removeUpdates(this); } @Override public void draw(Canvas canvas, MapView mapView, boolean shadow) { drawMyLocation(canvas, mapView, lastKnownLocation, lastKnownPoint, 0); } protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLoc, long when) { accuracyPaint = new Paint(); accuracyPaint.setAntiAlias(true); accuracyPaint.setStrokeWidth(2.0f); drawable = centerDrawable; width = drawable.getIntrinsicWidth(); height = drawable.getIntrinsicHeight(); center = new Point(); left = new Point(); Projection projection = mapView.getProjection(); double latitude = lastFix.getLatitude(); double longitude = lastFix.getLongitude(); float accuracy = lastFix.getAccuracy(); float[] result = new float[1]; Location.distanceBetween(latitude, longitude, latitude, longitude + 1, result); float longitudeLineDistance = result[0]; GeoPoint leftGeo = new GeoPoint((int) (latitude * 1e6), (int) ((longitude - accuracy / longitudeLineDistance) * 1e6)); projection.toPixels(leftGeo, left); projection.toPixels(myLoc, center); int radius = center.x - left.x; accuracyPaint.setColor(0xff6666ff); accuracyPaint.setStyle(Style.STROKE); canvas.drawCircle(center.x, center.y, radius, accuracyPaint); accuracyPaint.setColor(0x186666ff); accuracyPaint.setStyle(Style.FILL); canvas.drawCircle(center.x, center.y, radius, accuracyPaint); drawable.setBounds(center.x - width / 2, center.y - height / 2, center.x + width / 2, center.y + height / 2); drawable.draw(canvas); } public void enableMyLocation() { for (String s : mLocationManager.getProviders(true)) { mLocationManager.requestLocationUpdates(s, updateTime, updateDistance, this); } Location loc = null; for (String s : mLocationManager.getProviders(true)) { loc = mLocationManager.getLastKnownLocation(s); if (loc != null) { loc.setLatitude(DEFAULT_LOCATION[0]); loc.setLongitude(DEFAULT_LOCATION[1]); lastKnownLocation = loc; lastKnownPoint = createGeoPoint(loc); return; } } loc = new Location(LocationManager.GPS_PROVIDER); loc.setLatitude(DEFAULT_LOCATION[0]); loc.setLongitude(DEFAULT_LOCATION[1]); lastKnownLocation = loc; lastKnownPoint = createGeoPoint(loc); } public Location getLastKnownLocation() { return lastKnownLocation; } public GeoPoint getLastKnownPoint() { return lastKnownPoint; } public void onLocationChanged(Location location) { checkFirstRunnable(); this.lastKnownLocation = location; this.lastKnownPoint = createGeoPoint(location); replaceOverlay(createCenterOverlay(lastKnownPoint)); } public void onProviderDisabled(String provider) { ViewAdapter.showLongToast(mContext, "Your location provider has been disabled -- please reenable it"); } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status, Bundle extras) { if (status == LocationProvider.AVAILABLE) { } if (status == LocationProvider.OUT_OF_SERVICE) { ViewAdapter.showShortToast(mContext, "Location is temporarily out of service."); } if (status == LocationProvider.TEMPORARILY_UNAVAILABLE) { } } private void replaceOverlay(OverlayItem overlay) { mOverlays.clear(); mOverlays.add(overlay); populate(); } public boolean runOnFirstFix(Runnable runnable) { if (lastKnownLocation != null) { runnable.run(); return true; } firstFixRunnable = runnable; return false; } @Override public int size() { return mOverlays.size(); } public void updateLocation() { }
默认的是没有边框的 实现边框 可以通过不同的背景颜色是实现
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:layout_width="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:stretchColumns="*" android:background="#ff0000">
<TableRow android:background="#00ff00" android:layout_margin="2dip">
<Button android:id="@+id/button" android:text="+" android:background="#0000ff" android:layout_margin="2dip"/>
<TextView android:text="wahah" android:background="#0000ff" android:layout_margin="2dip"/>
<TextView android:id="@+id/amount" android:background="#0000ff" android:layout_margin="2dip"/>
</TableRow>
</TableLayout>