当前位置:  编程技术>移动开发
本页文章导读:
    ▪失去设备的ip        得到设备的ip 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).........
    ▪ 地图View添加多个overlayitem以及弹出对话框        mapView添加多个overlayitem以及弹出对话框 public class HelloGoogleMaps extends MapActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MapView mapVie.........
    ▪ TableLayout框子       TableLayout边框 默认的是没有边框的 实现边框 可以通过不同的背景颜色是实现 <?xml version="1.0" encoding="utf-8"?><TableLayout android:layout_width="wrap_content" xmlns:android="http://schemas.android.com/apk/res/and.........

[1]失去设备的ip
    来源: 互联网  发布时间: 2014-02-18
得到设备的ip

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        &amp; 0xFF) + "." +
    ((i &gt;&gt;  8 ) &amp; 0xFF) + "." +
    ((i &gt;&gt; 16 ) &amp; 0xFF) + "." +
        ((i &gt;&gt; 24 ) &amp; 0xFF)
        ;
    }


    
[2] 地图View添加多个overlayitem以及弹出对话框
    来源: 互联网  发布时间: 2014-02-18
mapView添加多个overlayitem以及弹出对话框
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() { 
 
} 

 


    
[3] TableLayout框子
    来源: 互联网  发布时间: 2014-02-18
TableLayout边框

默认的是没有边框的 实现边框 可以通过不同的背景颜色是实现

<?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>


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3